Descending order in Map and Multimap of C++ STL
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 write.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.