Hello,
I have a vector filled with a structure. Now, I do
pMyVec.erase (pMyVec.begin() + 2) for example.
Now.. my question. Will pMyVec[2] still work for the next item? or do I have to shift back the items following the iterator I erased?
Hello,
I have a vector filled with a structure. Now, I do
pMyVec.erase (pMyVec.begin() + 2) for example.
Now.. my question. Will pMyVec[2] still work for the next item? or do I have to shift back the items following the iterator I erased?
Y3K Network http://www.y3knetwork.com
Bringing the software of the future on the net.
I think when you erase elements from vectors the elements after it are moved up.
The index is still valid MyVec[2] is a copy of MyVec[3], MyVec[3] now contains MyVec[4] and so on. If you are going to be doing this a lot and you need to preserve order your best bet is with a list. If the order doesn't matter copy the last element to the element you want to delete and delete the last element.
Note that sometimes you get even better performance by swapping the element with the last element as often a swap can be implemented in terms of swapping pointers rather than a deep copyCode:MyVec[2] = MyVec.back(); MyVec.pop_back();