关于Vector中存放指针的问题
关于Vector中存放指针的问题,在进行清空的时候比较安全的一种做法是:
std::vector<ClassName *> ClassNameVec;
...push_back(new ClassName());
std::vector<ClassName *>::iterator ClassNameIt = ClassNameVec.begin();
for(;ClassNameIt != ClassNameVec.end();++ClassNameIt)
{
if(*ClassNameIt != NULL)
{
delete *ClassNameIt;
*ClassNameIt = NULL;
}
}
ClassNameVec.clear();
std::vector<ClassName *> ClassNameVec;
...push_back(new ClassName());
std::vector<ClassName *>::iterator ClassNameIt = ClassNameVec.begin();
for(;ClassNameIt != ClassNameVec.end();++ClassNameIt)
{
if(*ClassNameIt != NULL)
{
delete *ClassNameIt;
*ClassNameIt = NULL;
}
}
ClassNameVec.clear();