Reputation: 89
I am learning about std::vector::insert
from http://www.cplusplus.com/reference/vector/vector/insert/
Relevant part of the code:
int main ()
{
std::vector<int> myvector (3,100);
std::vector<int>::iterator it;
it = myvector.begin();
it = myvector.insert ( it , 200 );
myvector.insert (it,2,300);
// "it" no longer valid, get a new one:
it = myvector.begin();
Why is iterator not valid after .insert
operation?
Upvotes: 0
Views: 88
Reputation: 1
From this reference it says:
Causes reallocation if the new size() is greater than the old capacity(). If the new size() is greater than capacity(), all iterators and references are invalidated. Otherwise, only the iterators and references before the insertion point remain valid. The past-the-end iterator is also invalidated.
Upvotes: 3