Skip to content
Related Articles

Related Articles

Improve Article

Descending order in Map and Multimap of C++ STL

  • Difficulty Level : Medium
  • Last Updated : 28 Jun, 2021

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.

Example:

Input :  (10, "queen"), (20, "rose"),  (5," lion")
Output : (20, "rose"),  (10, "queen"), (5," lion")




// C++ program makes a map to store
// elements in descending order.
#include<bits/stdc++.h>
using namespace std;
  
int main()
{
    // Here if greater<int> is used to make
    // sure that elements are stored in
    // descending order of keys.
    map<int, string, greater <int> > mymap;
  
    // Inserting the elements one by one
    mymap.insert(make_pair(10, "queen"));
    mymap.insert(make_pair(20, "rose"));
    mymap.insert(make_pair(5," lion"));
  
    // begin() returns to the first value of map.
    map<int,string> :: iterator it;
    for (it=mymap.begin() ; it!=mymap.end() ; it++)
        cout << "(" << (*it).first << ", "
             << (*it).second << ")" << endl;
  
    return 0;
}

Output:



(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)




// C++ program makes a multimap to store
// elements in descending order.
#include<bits/stdc++.h>
using namespace std;
  
int main()
{
    // Here if greater<int> is used to make
    // sure that elements are stored in
    // descending order of keys.
    multimap<int, string, greater <int> > mymap;
  
    // Inserting the elements one by one
    mymap.insert(make_pair(10, "queen"));
    mymap.insert(make_pair(20, "rose"));
    mymap.insert(make_pair(5," lion"));
    mymap.insert(make_pair(20, "van")); // Duplicates allowed
    mymap.insert(make_pair(20, "watch"));
    mymap.insert(make_pair(5,"joker"));
  
  
    // begin() returns to the first value of multimap.
    multimap<int,string> :: iterator it;
    for (it=mymap.begin() ; it!=mymap.end() ; it++)
        cout << "(" << (*it).first << ", "
             << (*it).second << ")" << endl;
  
    return 0;
}

Output:

(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 review-team@geeksforgeeks.org. 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.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :