Removal of elements during iteration through a list - safety

c++, iteration

Solution

This is just fine because the erase method returns a new valid iterator.

Problem

I was wondering if something like this is safe... ``` // Iterating through a <list> while ( iter != seq.end()) { if ( test ) { iter = seq.erase( iter ); } else { ++iter; } ``` I know that iterating through a vector in this way would invalidate the iterator, but would the same thing occur in a list? I assume not since a list is sequential through pointers rather than being "next" to each other in memory, but any reassurance would be helpful.

Original source