Chapter 33. External Iterator Invalidation

 

Correctness must be a local property.

 
 --Neils Ferguson and Bruce Schneier
 

It pays to be obvious, especially if you have a reputation for subtlety.

 
 --Isaac Asimov

Element-Interface Coherence

As described in Section 1.2, the different standard containers have various rules for when and how their iterators (and pointers and references) may be invalidated in response to mutating changes on them. For example, in the following code, both b and e are invalidated:

std::vector<int>            ints;
ints.push_back(1);
ints.push_back(2);
std::vector<int>::iterator  b = ints.begin();
std::vector<int>::iterator  e = ints.end();
ints.erase(b); // b and e are invalidated

That’s because the standard dictates that all iterators of ...

Get Extended STL, Volume 1: Collections and Iterators now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.