0

Say you're iterating over a list using a nested for loop like this:

  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?
        }
      }
    }
  }

How can you maintain iter?

4

1 に答える 1

4

list:erase only invalidates the iterators pointing to the item being erased. Since iter is not equal to iter2, you should be fine.

于 2012-10-29T16:44:38.230 に答える