相关疑难解决方法(0)

用于remove_if的惯用C++

我有这门课

class Point2D
{
public:
 bool isValid();
 // ...
private:
 double x_, y_;
};
Run Code Online (Sandbox Code Playgroud)

我有一个std::vector< Point2D >,我想删除无效点,现在我喜欢这样:

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
}
Run Code Online (Sandbox Code Playgroud)

是否有一种标准的方法(精美),例如,不需要"复制"类方法的功能Point2D::isValid

也许使用C++ 11 lambda(我对lambda不是很熟悉)?

c++ c++11

10
推荐指数
5
解决办法
4534
查看次数

标签 统计

c++ ×1

c++11 ×1