In C++, say you have a std::map<int,int>
- how would you use stl
algorithms/libraries to find if there is a key
that satisfies a certain predicate, e.g. finding if there is a key
that is an odd number. So far I've got:
auto variable = std::find_if(my_map.begin(),my_map.end(), [](const auto& element) -> bool {return element%2 == 1;})
if (variable == my_map.end() .....
But how do I make sure the parameter in the predicate function is actually the key?
You can access the key via element.first
like this
const auto variable = std::find_if(my_map.begin(), my_map.end(), [](const auto& element) {
return element.first % 2 == 1;
}
);
if (variable != my_map.end()
{
// found
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With