Ok, I've run into this issue multiple times and thought it would be nice to throw this out to the good people on SO.
Say I have made a class, lets call it Resource. So the Resource class has a boolean variable which indicates weather any instance is active or not.
Now, I create a container which holds references to objects of the Resource type.
Over time , some of them get deactivated and I want to delete these deactivated objects so that they free up memory. Invariably I try to do this by : trying to iterate over the elements in the container and deleting the ones flagged as inactive. This , obviously leads to problems with iterator invalidation and the program starts throwing runtime errors.
So finally, my question is , what is the best method to safely delete objects depending on some condition that can only be evaluated by looking into the contents of the object.