Recently I started using range-based for loop. I had a problem where I needed to sort a map by value and then assign value to vector/array. Can I do that with this loop?
for (auto& it : M){
// value to vector
}
This question would be the same if I had a vector of pairs, could I just check for second, if it is larger then some other element or number? All this using range-based for loop.
>Solution :
From C++20, you can use views::values to get at the values of a std::map, or a vector<pair> for that matter:
for (auto v : m | std::views::values) // m is some map
// ...
You can similarly get at the keys with views::keys.