comparing map's key and sorted vector
Suppose I have a map (m) and a sorted vector of strings (v). The map's key is a string, and the map's value is foobar. I wish to compare map's keys with the vector's values and detect if they all match: (*mit).first == *vit and wish to do it with STL/Boost algo and functors. So I am starting with this: equal(m.begin(), m.end(), v.begin(), boost::bind(std::equa_to<string>(), std::select1st<std::pair<string,foobar> >(_1), _2)); I know 2 parameters are past to bind: *mit and *vit . However, before the equality, I want to extract the key from the map iterator (i.e. (*mit).first) before the comparison. That is this part I have difficulty to concretise. Btw, is there any equivalent to select1st (SGI extension) in Boost? Does anyone have any idea? I must be close to the solution...
Not directly, but Boost.Bind has a more powerful capability that can be used for this application. If, for the bound function, a pointer to member is provided, Boost.Bind will create a functor that takes a pointer to the class and returns a reference to the member. In your case, something like equal(m.begin(), m.end(), bind(std::equal_to<string>(), _1, bind(&map_type::value_type::first, _1))); At 03:55 PM 11/9/2006, you wrote:
Suppose I have a map (m) and a sorted vector of strings (v). The map's key is a string, and the map's value is foobar.
I wish to compare map's keys with the vector's values and detect if they all match:
(*mit).first == *vit
and wish to do it with STL/Boost algo and functors.
So I am starting with this:
equal(m.begin(), m.end(), v.begin(), boost::bind(std::equa_to<string>(), std::select1st<std::pair<string,foobar> >(_1), _2));
I know 2 parameters are past to bind: *mit and *vit . However, before the equality, I want to extract the key from the map iterator (i.e. (*mit).first) before the comparison. That is this part I have difficulty to concretise.
Btw, is there any equivalent to select1st (SGI extension) in Boost?
Does anyone have any idea? I must be close to the solution...
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Alan M. Carroll
-
Maitre Bart