O'Reilly logo

Extended STL, Volume 1: Collections and Iterators by Matthew Wilson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required