【发布时间】:2018-08-09 03:08:21
【问题描述】:
所有,
for( std::map<int, std::vector<Foo *> >::iterator it = fkFields.begin(); it != fkFields.end() && !found; it++ )
{
for( std::vector<Foo *>::iterator it1 = it->second.begin(); it1 < it->second.end(); ++it1 )
{
if( refTableOrig == (*it1)->GetReferencedTableName() )
{
found = true;
delete (*it1);
(*it1) = NULL;
it->second.erase( it1 );
}
}
if( found )
fkFields.erase( it );
}
当 std::vector 中只有 1 个元素时,上面的代码会崩溃,因为代码会尝试遍历 iterator::end()。
我也不能只使用vector.erase()/vector.remove(),因为向量包含指针,内存必须被删除。
那么从向量中删除指向元素的指针的正确方法是什么。
P.S.:这与所有其他问题不同,因为我的向量包含指针而不是对象。
TIA!!
【问题讨论】:
标签: c++11 pointers vector erase