Given a std::vector
, I want to delete all elements which match a condition. I cannot see any simple way to do this, as I cannot specify a single value for the elements to be deleted.
Here is a simple example illustrating the problem. I want to delete elements from v
that have their member k
equal to a given value.
int kDelete = 123; // elements to be deleted
struct S
{
int k;
int data;
};
vector<S> v;
for(auto& s:v)
{
if(s.k == k)
v.erase(???); // what is the correct arg for erase?
}
Prior answers are contradictory with respect to whether erase
invalidates implicit iterator, and whether the range limits are recalculated at each iteration. Also, they are several years old.