http://www.martinbroadhurst.com/iterator-invalidation-rules-for-c-containers.html has concise explanations. Specifically,
- list insertions don’t invalidate any iterator. I feel iterator is a pointer to a node.
- tree insertions don’t invalidate any iterator. Same reason as list.
- for erasure from lists or trees, only the iterator to the erased node is invalidated.
Now for vector:
- vector insertion invalidates any iterator positioned somewhere after the insertion point. If reallocation happens due to exceeding vector.capacity() then all invalidated
Who cares?
- in projects, we always check the documentation
- in coding interviews, we can save precious time if we remember these rules
- in QQ interviews, I have never received such a knowledge question. I think interviewers don’t consider this very “deep”, but we could possibly impress some interviews.