Descending Order in Map and Multimap of C++ STL



Generally, the default behavior of map and multimap map is to store elements is in ascending order. But we can store element in descending order by using the greater function.

Map in Descending Order

We use a map to store elements in descending order with the help of the greater function. We perform various operations like inserting elements, finding an element, counting occurrences of keys, and removing elements from the map. Functions are used here -

  • m::find() - Returns an iterator to the element with key value ?b' in the map if found, else returns the iterator to end.

  • m::erase() - Removes the key value from the map.

  • m:: equal_range() - Returns an iterator of pairs. The pair refers to the bounds of a range that includes all the elements in the container which have a key equivalent to key.

  • m insert() - To insert elements in the map container.

  • m size() - Returns the number of elements in the map container.

  • m count() - Returns the number of matches to an element with key value ?a' or ?f' in the map.

Example

In this example, we insert four key-value pairs into a map, sorted in descending order. We then display the map's size, list its elements, check the count of specific keys, search for a key, and erase an element. Finally, we display the updated map after modification.

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main () {
   map<char, int,greater <int>> m;
   map<char, int>::iterator it;
   m.insert (pair<char, int>('a', 10));
   m.insert (pair<char, int>('b', 20));
   m.insert (pair<char, int>('c', 30));
   m.insert (pair<char, int>('d', 40));
   cout<<"Size of the map: "<< m.size() <<endl;
   cout << "map contains:\n";
   for (it = m.begin(); it != m.end(); ++it)
      cout << (*it).first << " => " << (*it).second << '\n';
   for (char c = 'a'; c <= 'd'; c++) {
      cout << "There are " << m.count(c) << " element(s) with key " << c << ":";
      map<char, int>::iterator it;
      for (it = m.equal_range(c).first; it != m.equal_range(c).second; ++it)
         cout << ' ' << (*it).second;
      cout << endl;
   }
   if (m.count('a'))
      cout << "The key a is present\n";
   else
      cout << "The key a is not present\n";
   if (m.count('f'))
      cout << "The key f is present\n";
   else
      cout << "The key f is not present\n";
      it = m.find('b');
      m.erase (it);
      cout<<"Size of the map: "<<m.size()<<endl;
   cout << "map contains:\n";
   for (it = m.begin(); it != m.end(); ++it)
      cout << (*it).first << " => " << (*it).second << '\n';
   return 0;
}

Output

Below is the output of the above program:

Size of the map: 4
map contains:
d => 40
c => 30
b => 20
a => 10
There are 1 element(s) with key a: 10
There are 1 element(s) with key b: 20
There are 1 element(s) with key c: 30
There are 1 element(s) with key d: 40
The key a is present
The key f is not present
Size of the map: 3
map contains:
d => 40
c => 30
a => 10

Multimap in Descending Order

We use a multimap to store multiple elements with the same key in descending order using the greater function. Unlike a map, a multimap allows duplicate keys. We perform operations such as inserting elements, finding elements, and counting the occurrences of a key.

  • mm::find() - Returns an iterator to the element with key value ?b' in the multimap if found, else returns the iterator to end.

  • mm::erase() - Removes the key value from the multimap.

  • mm:: equal_range() - Returns an iterator of pairs. The pair refers to the bounds of a range that includes all the elements in the container which have a key equivalent to key.

  • mm insert() - To insert elements in the multimap container.

  • mm size() - Returns the number of elements in the multimap container.

Example

In this example, we show the insertion of multiple elements with duplicate keys into a multimap and store them in descending order. We check the count of occurrences of keys, find elements, and erase elements while printing the multimap's size and contents at each stage.

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main () {
   multimap<char, int,greater <char>> mm;
   multimap<char, int>::iterator it;
   mm.insert (pair<char, int>('a', 10));
   mm.insert (pair<char, int>('b', 20));
   mm.insert (pair<char, int>('a', 30));
   mm.insert (pair<char, int>('b', 40));
   cout<<"Size of the multimap: "<< mm.size() <<endl;
   cout << "multimap contains:\n";
   for (it = mm.begin(); it != mm.end(); ++it)
      cout << (*it).first << " => " << (*it).second << '\n';
   for (char c = 'a'; c <= 'd'; c++) {
      cout << "There are " << mm.count(c) << " elements with key " << c << ":";
      map<char, int>::iterator it;
   for (it = mm.equal_range(c).first; it != mm.equal_range(c).second; ++it)
      cout << ' ' << (*it).second;
      cout << endl;
   }
   if (mm.count('a'))
      cout << "The key a is present\n";
   else
      cout << "The key a is not present\n";
   if (mm.count('f'))
      cout << "The key f is present\n";
   else
      cout << "The key f is not present\n";
   it = mm.find('b');
   mm.erase (it);
   cout<<"Size of the multimap: "<<mm.size()<<endl;
   cout << "multiap contains:\n";
   for (it = mm.begin(); it != mm.end(); ++it)
      cout << (*it).first << " => " << (*it).second << '\n';
   return 0;
}

Output

Below is the output of the above program:

Size of the multimap: 4
multimap contains:
b => 20
b => 40
a => 10
a => 30
There are 2 elements with key a: 10 30
There are 2 elements with key b: 20 40
There are 0 elements with key c:
There are 0 elements with key d:
The key a is present
The key f is not present
Size of the multimap: 3
multiap contains:
b => 40
a => 10
a => 30

Conclusion

In this article, we showed how to store elements in descending order using map and multimap in C++. We explained how to insert, find, count, and remove elements, with easy examples and outputs for both containers. These actions help manage ordered data in C++.

Updated on: 2025-01-30T14:47:12+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements