remove_if的惯用C ++ [英] Idiomatic C++ for remove_if
本文介绍了remove_if的惯用C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个类
class Point2D
{
public:
bool isValid();
// ...
private:
double x_, y_;
};
我有一个 std :: vector< Point2D>
,我想删除无效点,现在我这样做:
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
}
这个(美观),例如不需要复制类方法的功能 Point2D :: isValid
?
Is there a standard way of doing this (beautifully), for example without the need of "duplicating" the functionality of the class method Point2D::isValid
?
也许使用C ++ 11 lambda(我不太熟悉lambda)?
Maybe using C++11 lambda (I am not very familiar with lambda)?
推荐答案
试试这个:
points.erase(std::remove_if(points.begin(),
points.end(),
std::not1(std::mem_fun_ref(&Point2D::isValid))),
points.end());
这篇关于remove_if的惯用C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文