结合boost unordered_map,bind和std :: find_if

时间:2015-06-04 10:13:21

标签: c++ boost

我有一个大容器boost :: unordered_map,如下所示:

typedef boost::unordered_map< vertex, entity > vertex_container; 

顶点类有一个表示其位置的坐标成员变量。 我有几个坐标point_(s)。我想搜索我的容器中是否存在任何顶点,以便vertex.coordinate = point。

类似的东西:

vertex_container::iterator it = std::find_if(v_container.begin(), v_container.end(), boost::bind(&Vertex::coordinate(), _1) == point);

但它失败了。

我试过了:

vertex_container::iterator it = std::find_if(v_container | boost::adaptors::map_keys(boost::bind(&vertex::coordinate(), _1)) == point);

error: cannot call member function ‘mesh::coordinate mesh::Vertex::coordinate() const’ without object.

我试图将boost unordered_map,bind和std :: find_if组合起来。

注意我只能使用C ++ 09标准并提升1.53.0版本。

2 个答案:

答案 0 :(得分:1)

您需要做的是首先将密钥绑定到unordered_map之外,然后再次绑定成员函数。

vertex_container::iterator it = std::find_if( v_container.begin(), v_container.end(), (boost::bind(&vertex::coordinate, (boost::bind( &vertex_container::value_type::first, _1))) == point) );

并且你也不能在std :: find_if中使用管道。

答案 1 :(得分:0)

在您的代码中,您有:

boost::bind(&Vertex::coordinate, _1) == point

这是boost::bind(...)和point之间的比较,因为它是一个比较,所以它是一个布尔值。或者更可能的是,您的编译器不知道如何比较这两者。

std::find_if接受一个函数返回boolean作为参数。

这意味着,因为你在&lt; {{}}上,你必须在某个地方声明一个函数:

c++11

然后您可以bool isVertexEqualToPoint(Vertex*, point){ return vertex.coordinate==point; } 将该功能与您的观点进行比较。 我确实认为在这里做一个比较对象更优雅。看看this question。它应该是直截了当的。只需用您自己的条件替换boost::bind