Here is the code snippet I was looking at:
vector<int> iv = {1,2,3,4,5,6,7,8,9,10}; auto iter = iv.begin(), mid = iv.begin() + iv.size()/2; for(int count = 100; count; --count ) { iter = iv.insert(iter, - 1); cout << "capacity = " << iv.capacity() << "*mid = " << *mid << endl; }
In accordance with the iterator invalidation rules:
vector: all iterators and links to the insertion point are not affected if the new container size is larger than the previous capacity (in this case, all iterators and links are invalid) [23.2.4.3/1] Iterator invalidity rules
I understand that since I override the value of "iter" in each insert operation, perhaps I can keep it valid (please correct me if I am wrong). However, the middle iterator remains valid in this case, even if I do not fake it in the loop, as well as when changing the capacity of the vector.
So how can the middle be updated after redistribution?
To find out if the environment has changed at all or not, I changed line 4 in the code to:
iv.insert(iter, -1); // Did not assign it back to iter.
Printing the results of dereferencing a value in the middle implies a change and, possibly, also that iter is invalid. (Again, please correct me if I am wrong).