algorithm - C++: Using boost lambda to get maximum values in a std::tr1::unordered_map -
i have std::tr1::unordered_map<int, a> map;
class member variable (int x).
i find key in map such map[i].x maximum.
i know can write functor go std::max_element. how do instead, using boost lambda (i trying learn it) ? not have c++0x.
as added question, if had class defined below:
class { int x; int y; };
and wanted find maximum w.r.t x (if maximum 0, find maximum wrt y). again, 1 solution iterate on tuples of map (keeping both maxima in memory). there way modify max_element ?
boost.lambda uses boost.bind access member variables. comes bit wordy:
typedef std::tr1::unordered_map<int, a> map_t; map_t m; max_a = std::max_element(m.begin(), m.end(), bind(&a::x, bind(&map_t::value_type::second, _1)) < bind(&a::x, bind(&map_t::value_type::second, _2)) )->second;
test: https://ideone.com/v6szl
you may save half headache boost.range
a max_a = *boost::max_element(m | map_values, bind(&a::x, _1) < bind(&a::x, _2));
but in practice, think, functor best.
Comments
Post a Comment