Return the return value of erase and use it as your iterator. The return value is an iterator of the next valid location after erasure.
if(ShouldErase) { iter = list.erase(iter); } else { ++iter; }
Reference
Excerpts:
Return value
A bidirectional iterator pointing to the new location of the element that followed the last element erased by a function call, which is the end of the list if the operation deleted the last element in the sequence.
Sion sheevok
source share