52

I'm having trouble iterating in reverse over a map in GCC C++. When I use a reverse iterator, it seems I can't assign anything to it - the compiler complains. I'm working around it with some awkward code using a forward iterator, but it's not very elegant. Any thoughts?

honk
  • 9,137
  • 11
  • 75
  • 83
Jack BeNimble
  • 35,733
  • 41
  • 130
  • 213
  • What you haven't told us is what you want to do. – tpdi Apr 08 '09 at 22:39
  • Sounds like you might be using a reverse iterator, but still calling begin and end rather than rbegin and rend. GMan's solution might be what you're looking for. But posting some code will help us help you. – Smashery Apr 08 '09 at 22:42
  • Thanks - I thought I had tried the rbegin/rend, but must have got myself tangled up somehow. – Jack BeNimble Apr 08 '09 at 23:32
  • You should check, how you obtain iterator range ( it should be rbegin()/rend() instead begin()/end() ). Do you really use reverse_iterator ( not const_reverse_iterator )? – bayda Apr 08 '09 at 22:46

2 Answers2

88

Here's an example of iterating backward through a std::map:

#include <iostream>
#include <map>
#include <string>

int main() {
    std::map<std::string, std::string> m;
    m["a"] = "1";
    m["b"] = "2";
    m["c"] = "3";

    for (auto iter = m.rbegin(); iter != m.rend(); ++iter) {
        std::cout << iter->first << ": " << iter->second << std::endl;
    }
}

If you are pre-C++11, you'll just need to spell out auto, which is:

std::map<std::string, std::string>::reverse_iterator

Note that if you're using boost, you can use a range-based for loop with a reverse adapter:

#include <boost/range/adaptor/reversed.hpp>

for (auto& iter : boost::adaptors::reverse(m)) {
    std::cout << iter.first << ": " << iter.second << std::endl;
}
GManNickG
  • 494,350
  • 52
  • 494
  • 543
  • 1
    is it possible to use `auto` here? – Chani Nov 21 '15 at 13:30
  • @Wildling: Yeah, this answer was posted before C++11. – GManNickG Nov 21 '15 at 23:08
  • 1
    Just in case if you are wondering how to work without auto iter, define: std::map::reverse_iterator instead of regular iterator. Took me a while to google out as compiler issue is not most helpful. – Coder Mar 30 '18 at 04:09
12

Since C++20 you can make use of the range adaptor std::views::reverse from the Ranges library. If you add this to a range-based for loop with structured binding, iterating backwards over an std::map could be done as follows:

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

int main() {
    std::map<std::string, int> m = { {"a", 1}, {"b", 2}, {"c", 3} };

    for (auto const& [k, v] : m | std::views::reverse)
        std::cout << k << " => " << v << std::endl;

    return 0;
}

Output:

c => 3
b => 2
a => 1

Code on Wandbox

honk
  • 9,137
  • 11
  • 75
  • 83