I am trying to iterate over an equal_range of a multimap using a range-based for loop. I'm imitating code I've seen posted, but getting an error.
#include <iostream>
#include <map>
int main() {
std::multimap<const unsigned long, const std::string> colors;
colors.insert({2UL, "red"});
colors.insert({2UL, "orange"});
colors.insert({3UL, "yellow"});
colors.insert({4UL, "green"});
colors.insert({4UL, "blue"});
colors.insert({5UL, "violet"});
for (const auto &it : colors.equal_range(4UL)) {
std::cout << " " << it.second << std::endl;
}
}
Here's what happens when compiling:
g++ --version
g++ (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)
g++
$g++ -o foo foo.cc
foo.cc: In function ‘int main()’:
foo.cc:14:49: error: no matching function for call to
‘begin(std::pair<std::_Rb_tree_iterator<std::pair<const long unsigned int, const std::__cxx11::basic_string<char> > >, std::_Rb_tree_iterator<std::pair<const long unsigned int, const std::__cxx11::basic_string<char> > > >&)’
for (const auto &it : colors.equal_range(4UL)) {
The last time I used C++ was before C++11. What am I doing wrong?
std::multimap::equal_range
returnsstd::pair<iterator,iterator>
It is not directly support from range-based for-loop.
with
c++20
, you can now use std::ranges::subrange to adapt it.WandBox Link