Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualiMalc

Posted 04 May 2013 - 11:52 PM

You can erase items from a list as your iterating it, you just need to stash the next iterator before you do stuff to the current one.

That still wont cut it in this scenario, as the OP says"

It could also result in removing other objects from the list

That would mean that it's possible that the iterator obtained before doStuffWith(it); pointed to the item which got removed, which means that iterator is now invalid.
In fact if the current item was not removed but the next item was, then you must only advance the iterator at the end of the iteration, as per normal iteration.

The problem is that in the case being described, there is no way to know whether the current item it being deleted or the next item is being deleted. So you don't know whether to get the advanced iterator at the beginning of or the end of each iteration.

Hence it is not solvable without at least some information about what is deleted coming back to the code in this loop. If you don't know the current item or the next item is always still there at the end of each iteration, then you can't do it.

#1iMalc

Posted 04 May 2013 - 11:50 PM

You can erase items from a list as your iterating it, you just need to stash the next iterator before you do stuff to the current one.

That still wont cut it in this scenario, as the OP says"

It could also result in removing other objects from the list

That would mean that it's possible that the iterator obtained before doStuffWith(it); pointed to the item which got removed, which means that iterator is now invalid.
In fact if the current item was not removed but the next item was, then you must only advance the iterator at the end of the iteration, as per normal iteration.

The problem is that in the case being described, there is no way to know whether the current item it being deleted or the next item is being deleted. So you don't know whether to get the advanced iterator at the beginning of or the end of each iteration.

Hence it is not solvable without at least some information about what is deleted coming back to the code in this loop.

PARTNERS