23.2.4/9 says of Associative Containers:
The insert and emplace members shall not affect the validity of
iterators and references to the container, and the erase members shall
invalidate only iterators and references to the erased elements
Now, there are some places where the standard talks about not invalidating "iterators and references to elements of the container", thus excluding end()
. I don't believe that this is one of them - I'm pretty sure that an end()
iterator is an "iterator to the container".
23.3.5.4/1 says for std::list
that insert
"Does not affect the validity of iterators and references", and 23.3.5.4/3 says that erase
"invalidates only the iterators and references to the erased elements". Again, end()
iterators are iterators and so their validity isn't excluded.
One thing to watch out for is that for any container, swap
can invalidate end() iterators (I assume this is because there are two "natural" behaviors, either that the end iterator points to the end of the same container or else to the end of the one it was swapped with, but the standard doesn't want to dictate which or rule out other possibilities). But you aren't swapping, just adding and removing elements.