H'H
H'H

Reputation: 1678

combine boost unordered_map, bind, and std::find_if

I have a big container of boost::unordered_map like below:

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

The vertex class has a coordinate member variable representing its location. I have several coordinate point_(s). I would like search if any vertex inside my container exist so that vertex.coordinate = point.

something like:

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

but it fails.

I tried:

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.

I somehow try to combine boost unordered_map, bind, and std::find_if.

Please NOTE I am only able to use C++09 standard and boost 1.53.0 version.

Upvotes: 1

Views: 461

Answers (2)

user2090491
user2090491

Reputation: 600

what you need to do is to first bind the key out of the unordered_map and then bind again the member function.

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) );

and also you cannot use pipe within std::find_if.

Upvotes: 1

David van rijn
David van rijn

Reputation: 2220

In your code you have :

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

this is a comparison between boost::bind(...) and point, and, since it is a comparison, it is a boolean value. Or more probably, your compiler doesn't know how to compare the two.

std::find_if takes a function returning boolean as argument.

This means, since you're on <c++11 you'll have to declare a function somewhere like:

bool isVertexEqualToPoint(Vertex*, point){
  return vertex.coordinate==point;
}

and then you can boost::bind that function to compare against your point. I do think however that is is more elegant here to make a compare object. Have a look at this question. It should be straightforward. Just replace the somehow_compare with your own condition.

Upvotes: 0

Related Questions