[英]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.