I'm trying to do something like this:
const std::map<char, int> histogram{
{'A', 2},
{'D', 1},
{'M', 1},
};
for (const auto& [index, key, value] : std::views::enumerate(histogram))
{
// ...
}
but it doesn't compile (Compiler Explorer) reporting error:
<source>:13:21: error: 3 names provided for structured binding
13 | for (const auto& [index, key, value] : std::views::enumerate(histogram))
| ^~~~~~~~~~~~~~~~~~~
<source>:13:21: note: while 'const std::tuple<long int, const std::pair<const char, int>&>' decomposes into 2 elements
Naively using [index, [key, value]] of course didn't work either...
Is there some other trick I'm missing or in this case I just cannot "unpack" the map element and instead have to keep it like [index, entry] and then use entry.first and entry.second?
Or maybe <ranges> offers something else than enumerate for this scenario?
std::vector<std::size_t>and use the (unsigned!) char values as indices directlyenumerateand structured binding, which might be useful in other cases as well. And my answer can be adapted to apply to anyenumerateresult.