I need a container that allows me to fast erasing ONE element while I am looping over it. I don’t need direct access because I always access it within a loop.
Is List faster than Vector for this case?
Pseudocode:
vector<Item*> myContainer;
for(..loop over it...) {
if (someCondition)
myContainer.erase(currentElement)
}
After having deleted an element I need to keep looping over the rest of elements
Yes, in theory lists offer faster removal than vectors, but in practice, nobody uses lists. Why not? Because vectors generate much less heap activity, and they offer much better cache locality.
Are you sure you absolutely need to loop over the vector and erase elements one by one? That would result in quadratic complexity for vectors, but the erase-remove-idiom does it in linear time:
(If the vector owns the Items, you should probably replace it with a
vector<unique_ptr<Item>>.)As an alternative, if you really must remove the elements one by one, and you don’t care about the relative order of the Items, there is a neat little trick to remove one element of a vector in constant time.