Same as Brian R. Bondy's answer, but I'd use a functor rather than a function
pointer because compilers are better at inlining them:
struct IsEven : public std::unary_function<int, bool>
{
bool operator()(int i)
{
return (i%2) == 0;
};
}
//...
std::erase(std::remove_if(v.begin(),v.end(),IsEven()), v.end());
EDIT: In response to
If my vector is of pointers that need to be freed after they are removed, how would I do this?
struct IsEven : public std::unary_function<int, bool>
{
bool operator()(int i)
{
return (i%2) == 0;
};
}
struct DeletePointer : public std::unary_function<myPointedType *, void>
{
void operator()(myPointedType * toDelete)
{
delete toDelete;
};
}
//...
typedef std::vector<something>::iterator Iterator_T;
Iterator_t splitPoint = std::partition(v.begin(),v.end(),IsEven());
std::for_each(v.begin(), splitPoint, DeletePointer());
v.erase(v.begin(), splitPoint);