c++ - How can I use Boost.Bind on compound types? -
i have std::map<int, std::pair<short, float> >
, , need find minimal short
in map. how can use boost::bind
std::min_element()
this?
boost::lambda
?
the map
iterator give pair
first
int
key , second
map's pair
value, if had iterator it
, you'd want minimum of it->second.first
values. min_element
function expects comparison function third argument, need build comparison function projects second.first
of 2 arguments.
we'll start typedefs make code more readable:
typedef std::pair<short, float> val_type; typedef std::map<int, val_type> map_type; map_type m;
we're going use boost.lambda overloaded operators, allowing use operator<
. boost.bind can bind member variables member functions, we'll take advantage of that, too.
#include <boost/bind.hpp> #include <boost/lambda/lambda.hpp> using boost::bind; // comparison (_1.second.first < _2.second.first) std::cout << std::min_element(m.begin(), m.end(), bind(&val_type::first, bind(&map_type::iterator::value_type::second, _1)) < bind(&val_type::first, bind(&map_type::iterator::value_type::second, _2)) )->second.first;
that work boost::lambda::bind
.
Comments
Post a Comment