Descending order in Map and Multimap of C++ STL

2

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
    // ascending 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
    // ascending 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 contribute.geeksforgeeks.org or mail your article to contribute@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.

GATE CS Corner    Company Wise Coding Practice

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

Recommended Posts:



2 Average Difficulty : 2/5.0
Based on 2 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.