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:
#include <algorithm>
myContainer.erase(
std::remove_if(
myContainer.begin(),
myContainer.end(),
[](Item* p) {
return p->someCondition;
}
),
myContainer.end()
);
(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.