Tutorialspoint.dev

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.

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)

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter