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?

bobobobo
  • 64,917
  • 62
  • 258
  • 363

1 Answers1

4

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

Matt Kline
  • 10,149
  • 7
  • 50
  • 87