std::vector
doesn't have a remove_if
member. But there is a standard algorith, aptly named std::remove_if
that can work in tandem with the vectors erase
member function.
users.erase(remove_if(begin(users), end(users), [val](User const& u)
{
return u.val == val;
}), end(users));
First remove_if
shifts the elements of the range, and returns an iterator past the end of all the "good ones". The vector is of the same size as when we started at this point. Now that iterator is fed to erase
, which kills all of those items, from that "new end", to the "old end" of the vector.
The reason std::list
implements its own member version of remove_if
is to better utilize its node based structure. It doesn't have to do copies to shift elements around or delete them. For std::vector
, the algorithm is the best we can do.