Writing this per request.
PS: This is a follow-up question to Deleting elements from a vector
This is a troubling pattern in your line of questioning.
Seriously, modifying containers while you're iterating over them is bad juju. It is difficult to get right. It often doesn't save you much in terms of performance. It's rarely (except in limited cases where there's a standard library algorithm exactly suited to your needs) easier to express. It can be very costly in performance if you do it wrong. It can be very costly in terms of program correctness if you do it wrong, which is much more important than anything else.
It gets exponentially worse if threading is involved. Java has a dedicated exception type specifically for certain things that can go wrong when you attempt this. In C++ you aren't so lucky; the analogous problem might not be detectable.
Just write code that produces the final result, then replace the original container. The only thing that can really slow this down is "instances are expensive to copy", in which case you should already be using a layer of indirection to handle the problem (assuming it really matters) anyway.
list<Elem> original;
// fill elems with some Elems;
list<Elem> modified;
for (
list<Elem>::iterator it = original.begin(), end = original.end();
it != end; ++it
) {
if (something()) {
modified.push_back(*it);
} else if (something_else()) {
modified.push_back(Elem()); // for example
}
// else, 'erase' the element by just not putting anything into 'modified'
// Or we could do whatever other combination of things,
// maybe insert more than one
}
std::swap(original, modified);