This question is inspired from another topic which poses this question:
Find the first value greater than user specified value from a map container
which can be solved in several ways. A typical C++03 solution defines a dedicated function (or functor) and pass it to std::find_if
as third argument.
In C++11, one can avoid defining a dedicated function (or functor), and can instead make use of lambda
as:
auto it = std:: find_if(m.begin(), mp.end(),
[n](const std::pair<std::string, int> & x) -> bool
{ return x.second > n; }
);
which is the accepted answer.
I'm still looking for a short and cool solution. If it were a vector, then I just learnt a cool solution which makes use of Boost.Phoenix
and the solution becomes very concise (ideone demo):
std::vector<int> v = ...;
auto it = std::find_if(v.begin(), v.end(), arg1 > 4);
Here arg1
is a functor object defined in boost::phoenix::arg_names
namespace, and the expression arg1>4
evaluates to another functor which then gets passed to std::find_if
.
A quick test is (ideone),
std::cout<< (arg1 > 9)(v) << std::endl; //prints 0 if as v > 9 is false, else 1
//or store the functor first and then use it
const auto & f = arg1 > 9;
std::cout<< f(v) << std::endl; //prints 0 if as v > 9 is false, else 1
My question is, I want to solve the map problem, in a similar way. Is there any such solution? Something like:
auto it = std::find_if(m.begin(),mp.end(), (???).second > n); //m is std::map
Or,
auto it = std::find_if(m.begin(),mp.end(), at<1>(arg1) > n); //m is std::map
For it to work, the expression at<1>(arg1) > 2
has to evaluate to a functor which takes const std::pair &
as argument. My gut feelings tells me that boost has this solution. :-)
std::find_if
which returns an iterator of the map. – ErepsinReturns an iterator pointing to the first element in the container whose *key* compares greater than x (using the container's comparison object).
and here I want ot compare values, not keys. – Erepsin