I've not found a tidy, all-in-one table of all the C++ 2003 container iterator invalidation rules.
So, with references, here they are!
Insertion
Container | Rules | 2003 ref. |
---|---|---|
Sequence containers | ||
vector |
all iterators and references before the point of insertion are unaffected, unless the new container size is greater than the previous capacity (in which case all iterators and references are invalidated) | 23.2.4.3/1 |
deque |
all iterators and references are invalidated, unless the inserted member is at an end (front or back) of the deque (in which case all iterators are invalidated, but references to elements are unaffected) | 23.2.1.3/1 |
list |
all iterators and references unaffected | 23.2.2.3/1 |
Associative containers | ||
set |
all iterators and references unaffected | 23.1.2/8 |
multiset |
||
map |
||
multimap |
||
Container adaptors | ||
stack |
inherited from underlying container | |
queue |
||
priority_queue |
Erasure
Container | Rules | 2003 ref. |
---|---|---|
Sequence containers | ||
vector |
every iterator and reference after the point of erase is invalidated | 23.2.4.3/3 |
deque |
all iterators and references are invalidated, unless the erased members are at an end (front or back) of the deque (in which case only iterators and references to the erased members are invalidated) | 23.2.1.3/4 |
list |
only the iterators and references to the erased element is invalidated | 23.2.2.3/3 |
Associative containers | ||
set |
only iterators and references to the erased elements are invalidated | 23.1.2/8 |
multiset |
||
map |
||
multimap |
||
Container adaptors | ||
stack |
inherited from underlying container | |
queue |
||
priority_queue |
Resizing
Container | Rules | 2003 ref. |
---|---|---|
Sequence containers | ||
vector |
as per insert/erase | 23.2.4.2/6 |
deque |
as per insert/erase | 23.2.1.2/1 |
list |
as per insert/erase | 23.2.2.2/1 |
Bootnote
Unless otherwise specified (either explicitly or by defining a function in terms of other functions), invoking a container member function or passing a container as an argument to a library function shall not invalidate iterators to, or change the values of, objects within that container. [23.1/11]
Bootnote 2
It's not clear in C++2003 whether "end" iterators are subject to the above rules; you should assume, anyway, that they are (as this is the case in practice).
(this post can also be found on Stack Overflow)
(also see the same post regarding C++0x)