Listing 3: Illustrates iterator validity
// Conditional erasing from a deque // std::deque<int> deq; // Put some stuff into deq std::deque<int>::iterator deq_current = deq.begin(); while(deq.end() != deq_current) { if( /* some condition */ ) deq_current = deq.erase(deq_current); else ++deq_current; } // Conditional erasing from a list // std::list<int> li; // Put some stuff into li std::list<int>::iterator li_current = li.begin(); std::list<int>::iterator li_end = li.end(); while(li_end != li_current) { if( /* some condition */ ) li_current = li.erase(li_current); else ++li_current; } End of Listing