How to traverse a STL map in reverse direction?

Map stores the elements in sorted order of keys. Now if we want to traverse it in reverse order we will use reverse_iterator of map.

Syntax:

```map::reverse_iterator iterator_name;
```

Reverse Iterator of map moves in backward direction on increment. So, we will point the reverse_iterator to the last element of map and then keep on incrementing it until it reaches the first element. To do this we will use 2 member functions of std::map i.e.
1. rbegin() : It returns the reverse_iterator pointing to last element of map.
2. rend() : It returns the reverse_iterator pointing to first element of map.

Now for traversing in reverse order we will iterate over the range b/w rbegin() & rend() using reverse_iterator.
Reverse Iteration in map:
Example:

```Input:  (10, "geeks"), (20, "practice"),  (5, " contribute")
Output : (20, "practice"),  (10, "geeks"), (5, " contribute")
```

 `// C++ program makes a map to iterate ` `// elements in reverse order. ` `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` ` `  `    ``// Creating & Initializing a map of String & Ints ` `    ``map<``int``, string> mymap; ` ` `  `    ``// Inserting the elements one by one ` `    ``mymap.insert(make_pair(10, ``"geeks"``)); ` `    ``mymap.insert(make_pair(20, ``"practice"``)); ` `    ``mymap.insert(make_pair(5, ``"contribute"``)); ` ` `  `    ``// Create a map reverse iterator ` `    ``map<``int``, string>::reverse_iterator it; ` ` `  `    ``// rbegin() returns to the last value of map ` `    ``for` `(it = mymap.rbegin(); it != mymap.rend(); it++) { ` `        ``cout << ``"("` `<< it->first << ``", "`  `             ``<< it->second << ``")"` `<< endl; ` `    ``} ` ` `  `    ``return` `0; ` `} `

Output:

```(20, practice)
(10, geeks)
(5, contribute)
```

We can also use auto to avoid remembering complex syntax.

 `// C++ program makes a map to iterate ` `// elements in reverse order with simpler ` `// syntax ` `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``// Creating & Initializing a map of String & Ints ` `    ``map<``int``, string> mymap; ` ` `  `    ``// Inserting the elements one by one ` `    ``mymap.insert(make_pair(10, ``"geeks"``)); ` `    ``mymap.insert(make_pair(20, ``"practice"``)); ` `    ``mymap.insert(make_pair(5, ``"contribute"``)); ` ` `  `    ``// rbegin() returns to the last value of map ` `    ``for` `(``auto` `it = mymap.rbegin(); it != mymap.rend(); it++) { ` `        ``cout << ``"("` `<< it->first << ``", "`  `             ``<< it->second << ``")"` `<< endl; ` `    ``} ` ` `  `    ``return` `0; ` `} `

Output:

```(20, practice)
(10, geeks)
(5, contribute)
```

Reverse Iteration in multimap:
Multimap is similar to map with an addition that multiple elements can have same keys. Rather than each element being unique, the key value and mapped value pair has to be unique in this case.

Example:

```Input :  (10, "geeks"), (20, "practice"),  (5, "contribute"),
(20, "van"), (20, "watch"), (5, "joker")
Output:  (20, "watch"), (20, "van"), (20, "practice"),
(10, "geeks"), (5, "joker"), (5, "contribute")
```

 `// C++ program makes a multimap to store ` `// elements in descending order. ` `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` ` `  `    ``// Creating & Initializing a multimap  ` `    ``// of Ints & String ` `    ``multimap<``int``, std::string> mymap; ` ` `  `    ``// Inserting the elements one by one ` `    ``mymap.insert(make_pair(10, ``"geeks"``)); ` `    ``mymap.insert(make_pair(20, ``"practice"``)); ` `    ``mymap.insert(make_pair(5, ``"contribute"``)); ` ` `  `    ``// Duplicates allowed ` `    ``mymap.insert(make_pair(20, ``"van"``));  ` ` `  `    ``mymap.insert(make_pair(20, ``"watch"``)); ` `    ``mymap.insert(make_pair(5, ``"joker"``)); ` ` `  `    ``for` `(``auto` `it = mymap.rbegin(); it != mymap.rend(); it++) { ` `        ``cout << ``"("` `<< it->first << ``", "`  `            ``<< it->second << ``")"` `<< endl; ` `    ``} ` ` `  `    ``return` `0; ` `} `

Output:

```(20, watch)
(20, van)
(20, practice)
(10, geeks)
(5, joker)
(5, contribute)
```

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.