I have this class
class Point2D
{
public:
bool isValid();
// ...
private:
double x_, y_;
};
I have a std::vector< Point2D >
and I would like to remove the invalid points, now I do like this:
bool invalid ( const Point2D& p )
{
return !p.isValid();
}
void f()
{
std::vector< Point2D > points;
// fill points
points.erase( std::remove_if( points.begin(), points.end(), invalid ), points.end() );
// use valid points
}
Is there a standard way of doing this (beautifully), for example without the need of "duplicating" the functionality of the class method Point2D::isValid
?
Maybe using C++11 lambda (I am not very familiar with lambda)?
Try this:
points.erase(std::remove_if(points.begin(),
points.end(),
std::not1(std::mem_fun_ref(&Point2D::isValid))),
points.end());
Not totally standard but nearly : you can use boost::bind and do the following
points.erase( std::remove_if( points.begin(), points.end(),
!boost::bind(&Point2D::isValid, _1 )), points.end() );
By the way you should declare the isValid method const.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With