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
  • 61,493
  • 58
  • 247
  • 347

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
  • 9,611
  • 6
  • 47
  • 81