live element
allocated, no object
moved-from
invalid or UB
Costs
What invalidates iterators and references
Quirks worth knowing
Invalidation at a glance
What an insertion or erasure does to existing iterators and references into the container.
| container | insert | erase |
|---|---|---|
| vector, string | all, if it reallocates; otherwise those at or after the insertion point | those at or after the erased element |
| deque | at either end: all iterators, but no references; in the middle: all | at either end: only the erased; in the middle: all |
| list, forward_list | none | only the erased |
| set, map, multi* | none | only the erased |
| unordered_* | iterators, if it rehashes; references never | only the erased |
| flat_map, flat_set | all | all |
| span, string_view | whatever invalidates the underlying storage | |