Skip to content
Related Articles

Related Articles

Descending Order in Map and Multimap of C++ STL

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 07 Jan, 2022
View Discussion
Improve Article
Save Article

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 store elements in reverse order or descending order when inserting in map and multimap?

We can use the third parameter, that is std::greater along with map and multimap to store elements in descending order.
Descending order in the map: 

A map stores key-value pairs. A self-balancing-BST (typically Red-Black tree) is used to implement it.

Syntax:

map<key_datatype, value_datatype, greater<int> > mapName;

Example: 

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

Here, we want to save the elements in descending order, i.e. 20> 10> 5.

CPP




// C++ program makes a map to store
// elements in descending order
#include <bits/stdc++.h>
using namespace std;
 
// Driver Code
int main()
{
     
    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)

Here, if greater<int> is used to make sure that elements are stored in descending order of keys. Also, the following functions have been used here:

  • insert(): Inserts elements in the map container.
  • begin(): Returns an iterator to the first element in the map
  • end(): Returns an iterator to the theoretical element that follows the last element in the map

Descending order in multimap:
 
Multimap is similar to a map with the addition that multiple elements can have the same keys. Rather than each element is unique, the key-value and mapped value pair have to be unique in this case.

Syntax:

multimap<key_datatype, value_datatype, greater<int> > multimapName;

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)

CPP




// C++ program makes a multimap to store
// elements in descending order.
#include <bits/stdc++.h>
using namespace std;
 
// Driver Code
int main()
{
 
    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)

Here, if greater<int> is used to make sure that elements are stored in descending order of keys. Also, the following functions have been used here:

  • insert(): Inserts elements in the map container.
  • begin(): Returns an iterator to the first element in the map
  • end(): Returns an iterator to the theoretical element that follows the last element in the map

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.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!