// This is an example of iterator invalidation. It creates a container, // obtains an iterator to the one and only element in the container, and then // invalidates the iterator. #include #include using namespace std; int main() { vector bunch = {253}; vector::iterator it=bunch.begin(); // Iterator refers to first int // or use auto cout << "Must be 253: " << *it << '\n'; for (int i=1; i<10000; i++) // Add a whole mess of ints bunch.push_back(i); // which will force reallocation cout << "Probably not 253: " << *it << '\n'; // it’s been invalidated }