Reputation: 357
I am trying to delete a object from a vector at a specific index. The vector iterator keeps track of the index throughout the program. In the below code, the first IF statement works perfectly. But, if the iterator is pointing to anywhere OTHER than the last element, I erase the element from the vector and then increment the iterator. The program crashes and says "iterator not incrementable".
I ran the debugger several times and everything looks correct, so I cannot see what I am missing?
vector<Card> myVector; //container to hold collection of cards.
vector<Card>::iterator myVectorIterator; //points to each "card" in the collection.
Card Collection::remove()
{
if (myVectorIterator== myVector.end()-1) { //at the last card
//erase the "current" card
myVector.erase(myVectorIterator);
//update to the first card.
myVectorIterator= myVector.begin();
}
else
{
myVector.erase(myVectorIterator);
//crashes here!
myVectorIterator++;
}
return *myVectorIterator;
}
Upvotes: 0
Views: 909
Reputation: 1825
This is because the call to erase invalidates all iterators. Imagine you have something pointing to an element and that element disappears, what shall you point to?
Worse still, the behavior is highly implementation dependent. The correct approach is to store use the return value from erase which will be an iterator to the next element in the vector.
myVectorIterator = myVector.erase(myVectorIterator);
Note that you now have to remove the incrementation on the next line (or you will skip an item.)
In general, when manipulating different STL containers, you will see that in the documentation whether a given operation will have an effect on iterators. If you take a look at this link, you will see that for vector::erase this is the case:
Different containers may have different guarantees when it comes to iterator validity.
Upvotes: 1
Reputation: 254441
erase
invalidates the iterator, so you can't use it afterwards. But it helpfully returns an iterator to the element after the removed one:
myVectorIterator = myVector.erase(myVectorIterator);
Upvotes: 4
Reputation: 117856
You have to do this
myVectorIterator = myVector.erase(myVectorIterator);
It will remove the current iterator then assign the next item into it.
Upvotes: 0