![](/img/trans.png)
[英]avoid temporary std::string to invoke boost::unordered_map::find
[英]combine boost unordered_map, bind, and std::find_if
我有一个很大的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.
我以某种方式尝试结合升压unordered_map,bind和std :: find_if。
请注意,我只能使用C ++ 09标准并提升1.53.0版本。
您需要做的是首先将键从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中使用管道。
在您的代码中,您有:
boost::bind(&Vertex::coordinate, _1) == point
这是boost::bind(...)
和point之间的比较,并且由于是比较,所以它是布尔值。 或更可能是您的编译器不知道如何比较两者。
std::find_if
采用返回布尔值的函数作为参数。
这意味着,由于您使用的是< c++11
,因此必须在以下位置声明一个函数:
bool isVertexEqualToPoint(Vertex*, point){
return vertex.coordinate==point;
}
然后您可以boost::bind
该功能与您的观点进行比较。 我确实认为,在此处做一个比较对象更为优雅。 看看这个问题 。 它应该很简单。 只需将somehow_compare
替换为您自己的条件即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.