Open In App
Related Articles

Descending Order in Map and Multimap of C++ STL

Improve Article
Improve
Save Article
Save
Like Article
Like

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)

Time Complexity: O(nlogn)

Insertion operation in a map is a O(n) operation and if there are n elements then total time required will be O(n*logn).

Space complexity: O(n)

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)

Time Complexity: O(nlogn)

Insertion operation in a map is a O(n) operation and if there are n elements then total time required will be O(n*logn).

Space complexity: O(n)

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

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.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 08 Mar, 2023
Like Article
Save Article
Similar Reads