K
Kai-Uwe Bux
Stephen said:As I understand it, that may avoid invalidating the iterator in
practice, but not according to the standard.
That is, I thought that any modification of a standard library
container (other than a simple in-place overwrite of a value) is
considered to invalidate all iterators that refer into that container.
It's one of the reasons I use my own associative containers for a lot
of high-level work. High level code shouldn't be worrying about these
kinds of safety issues - it should just get on with the task at hand.
A (very small) performance penalty is worth paying to have safe
iterators that remain valid when the container is modified.
Not necessarily true in low level code, of course.
In my previous post, I was talking about sequences only and I forgot and
snipped the issue about associative containers. [23.1.2/8] says about them:
The insert 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.
So, when working with associative containers, one gets essentially the same
guarantees as with std::list. Iterator invalidation should not be a reason
to use your own associative containers. (That the library needs complete
types or other issues might still factor in.)
Best
Kai-Uwe Bux