主要分两种情况:存储的内容是指针;存储的内容是实际对象。
看下面两段代码,
typedef pair<VirObjTYPE, std::list<CheckID>*> VirObj_CheckID_pair;
class LangChecker
{
public:
LangChecker();
~LangChecker();
void Register(VirObjTYPE type, CheckID id);
private:
std::map<VirObjTYPE, std::list<CheckID>*> _registered_checker;
std::map<VirObjTYPE, std::list<CheckID>*>::iterator _registered_iter;
};
void LangChecker::Register(VirObjTYPE type, CheckID id)
{
_registered_iter = _registered_checker.find(type);
if(_registered_iter == _registered_checker.end()) //not found
{
std::list<CheckID>* newlist = new list<CheckID>;
(*newlist).push_back(id);
_registered_checker.insert(VirObj_CheckID_pair(type, newlist));
}
else
{
(*(*_registered_iter).second).push_back(id);
}
}
LangChecker::~LangChecker()
{
for(_registered_iter = _registered_checker.begin();
_registered_iter != _registered_checker.end(); _regeristered_iter++)
{
delete (*_registered_iter).second;
}
}
std::map<VirObjTYPE, std::list<CheckID>> _registered_checker;
std::map<VirObjTYPE, std::list<CheckID>>::iterator _registered_iter;
void LangChecker::Register(VirObjTYPE type, CheckID id)
{
_registered_iter = _registered_checker.find(type);
if(_registered_iter == _registered_checker.end()) //not found
{
std::list<CheckID>* newlist = new list<CheckID>;
(*newlist).push_back(id);
_registered_checker.insert(VirObj_CheckID_pair(type, *newlist));
delete newlist;
}
else
{
(*_registered_iter).second.push_back(id);
}
}
STL容器存储的内容动态分配情况下的内存管理,布布扣,bubuko.com
原文地址:http://blog.csdn.net/vintage_1/article/details/38439853