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
#include <bits/stdc++.h>
using namespace std;
int main()
{
map< int , string, greater< int > > mymap;
mymap.insert(make_pair(10, "queen" ));
mymap.insert(make_pair(20, "rose" ));
mymap.insert(make_pair(5, " lion" ));
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
#include <bits/stdc++.h>
using namespace std;
int main()
{
multimap< int , string, greater< int > > mymap;
mymap.insert(make_pair(10, "queen" ));
mymap.insert(make_pair(20, "rose" ));
mymap.insert(make_pair(5, " lion" ));
mymap.insert(make_pair(20, "van" ));
mymap.insert(make_pair(20, "watch" ));
mymap.insert(make_pair(5, "joker" ));
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!