Reputation: 68648
Suppose I have some function:
void mutate(V& v);
that reads/writes v
-
and I want to write a function:
void mutate_map_values(std::multimap<K,V>& m, K k);
that applies mutate
to all values of m
that have key k
.
What's the most succinct way to implement mutate_map_values
in C++20?
Upvotes: 2
Views: 236
Reputation: 170084
std::ranges::subrange
is a utility class that's constructible from anything that is like a pair of iterators. Which fits with what std::multimap::equal_range
already returns. Combining the two, we may write the desired function as follows in C++20:
#include <ranges>
void mutate_map_values(std::multimap<K,V>& m, K k) {
using namespace std::ranges;
for (auto& [_, v] : subrange(m.equal_range(k))) {
mutate(v);
}
}
Upvotes: 5