C++ multimap with range-based for loop iterating over equal_range: error message

1.8k views Asked by At

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?

1

There are 1 answers

0
apple apple On

std::multimap::equal_range returns std::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.

#include <iostream>
#include <map>
#include <ranges>

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 (auto [begin,end]=colors.equal_range(4UL); const auto &it : std::ranges::subrange{begin, end}) {
        std::cout << "    " << it.second << std::endl;
    }
}

WandBox Link