0

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?

ghostarbeiter
  • 815
  • 8
  • 7
  • From information at the linked (duplicate) answer, it seems that this was actually legal code in a draft version of C++11. The web pages where I saw similar code posted must have been using an early version of a compiler based on the draft. – ghostarbeiter Sep 07 '18 at 15:07

1 Answers1

1

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

apple apple
  • 10,292
  • 2
  • 16
  • 36