I have a container that looks like this: std::map<size_t, std::set<char>>
I want to find the absolute max element and the min element above a threshold x
based on the size of the std::set
.
I can get the max and min using the following line:
auto minMaxElements = std::minmax_element(cbegin(map), cend(map),
[](const auto &lhs, const auto &rhs){
return lhs.second.size() < rhs.second.size();
});
Is it possible to get the min element above a threshold x
using std::minmax_element?
Example: Say I have a std::map
of 5
elements. The sizes of the std::set
values are as 1, 2, 1, 10, and 5
. And I set the threshold as 1
, then I want the max element to give the iterator containing std::set
of size 10
and the min element to give the iterator containing std::set
of size 2
.
You can use std::find_if
to do that
auto it = std::find_if(map.begin(), map.end(),
[x](const auto & l) -> bool {
return l.second.size() >= x; // >= or >, depending upon requirement
}
);
Then use this iterator in your std::minmax_element
auto minMaxElements = std::minmax_element(it, map.end(),
[](const auto & lhs, const auto & rhs) {
return lhs.second.size() < rhs.second.size();
}
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.