假设您使用嵌套的for循环遍历列表,如下所示:
for( list<Object>::iterator iter = list.begin() ; iter != list.end() ; ++iter )
{
for( list<Object>::iterator iter2 = list.begin() ; iter2 != list.end() ; ++iter2 )
{
if( iter != iter2 )
{
if( some other condition )
{
iter2 = list.erase( iter2 ) ;
// uh oh! what about iter?
}
}
}
}
如何维护iter
?
答案 0 :(得分:4)
list:erase only invalidates the iterators pointing to the item being erased。由于iter
不等于iter2
,因此您应该没问题。