In this particular case you've done nothing to the iterator, but much rather to the object within it, so yes: The iterator remains valid.
But if you look at std::list::erase
, it sports a line such as "References and iterators to the erased elements are invalidated. Other references and iterators are not affected."
So if you tried to do *iter
after erase
, it would cause your program to fail.
This may seem obvious for erase
, but there are other operations where it is not as obvious.
For std::list
for example, the reference page says:
Adding, removing and moving the elements within the list or across several lists does not invalidate the iterators or references. An iterator is invalidated only when the corresponding element is deleted.
For std::vector
on the other hand, the reference for the push_back
method says:
If the new size() is greater than capacity() then all iterators and references (including the past-the-end iterator) are invalidated. Otherwise only the past-the-end iterator is invalidated.
That means, unlike with std::list
, it is not generally safe to keep an iterator to an element around, if the vector grows (because the underlying storage location of the item changes).