We have discussed map in C++ STL and multimap in C++ STL. The default behavior of these data structures is to store elements in ascending order. How to ensure reverse order or descending order when inserting elements in map and multimap.
The idea is to greater function when an instance of map/multimap.
Descending order in map:
A map stores key value pairs. A self-balancing-BST (typically Red-Black tree) is used to implement it.
Input : (10, "queen"), (20, "rose"), (5," lion") Output : (20, "rose"), (10, "queen"), (5," lion")
(20, rose) (10, queen) (5, lion)
Descending order 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, "queen"), (20, "rose"), (5," lion"), (20, "van"), (20, "watch"), (5, "joker") Output : (20, rose), (20, van), (20, watch), (10, queen), (5, lion), (5, joker)
(20, rose) (20, van) (20, watch) (10, queen) (5, lion) (5, joker)
This article is contributed by Jatin Goyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- How to sort an Array in descending order using STL in C++?
- How to sort a Vector in descending order using STL in C++?
- Sorting of Vector of Tuple in C++ (Descending Order)
- Sorting 2D Vector in C++ | Set 2 (In descending order by row and column)
- Print numbers in descending order along with their frequencies
- Sorting Vector of Pairs in C++ | Set 2 (Sort in descending order by first and second)
- multimap::crbegin() and multimap::crend() in C++ STL
- multimap::cbegin() and multimap::cend() in C++ STL
- multimap::begin() and multimap::end() in C++ STL
- multimap key_comp in C++ STL
- multimap equal_range() in C++ STL
- multimap find() in C++ STL
- multimap::erase() in C++ STL
- multimap::emplace() in C++ STL
- multimap::count() in C++ STL
Improved By : ArunrajShanmugam