Has anyone found a way to remove any item from a list as it is iterating?
Normally a loop to iterate through a linked list looks like this:
for ( it = list.begin(); it != list.end(); it++ ) { }
If you want to be able to remove an item from the list while it is iterating, you can always do this:
it = list.begin(); while (it != list.end() ) { if (condition) { it = list.erase(it); } else { it++; } }
But what if the code in your loop called other code that could result in the removal of the object from the list …