This is the 10th Part of Tutorial series on std::map in C++. In this tutorial, we’ll explore how to store keys in reverse order in a map in C++.

In standard C++ programming, std::map automatically sorts its keys in ascending order based on their values. However, there may be scenarios where we need to store keys in reverse order, typically in descending order. The common requirement is to have a map where the keys are sorted in a way that the largest key comes first and the smallest key comes last.

For example, if we have a map with integer keys {3, 1, 4, 2}, the default std::map would store them as {1, 2, 3, 4}. Our objective is to store them in reverse order as {4, 3, 2, 1}.

This can be done in two ways, let’s discuss them one by one

Technique 1: Using Custom Comparator

We can use a custom comparator in the declaration of the std::map to store the keys in reverse order. This comparator will define the ordering of the keys. In this case, we want to sort the keys in descending order, but by default, the map uses the std::less<> function. So, we will provide std::greater<int> as a comparator when creating the map object. This way, key-value pairs in the map will be sorted in decreasing order of keys.

#include <iostream>
#include <map>
#include <functional> 

int main() 
{
    // Create a map with a custom comparator
    std::map<int, std::string, std::greater<int>> mapObject;

    // Add elements to the map
    mapObject[3] = "Three";
    mapObject[1] = "One";
    mapObject[4] = "Four";
    mapObject[2] = "Two";

    // Iterate and print the elements of the map
    for (auto const &pair: mapObject)
    {
        std::cout << pair.first << " => " << pair.second << std::endl;
    }

    return 0;
}

Output:

4 => Four
3 => Three
2 => Two
1 => One

In this example, std::greater<int> is used as a comparator which results in the map storing the keys in descending order.

Technique 2: Using C++17 std::map with std::greater<>

With C++17, the standard library introduces the std::greater<> transparent comparator. It allows us to create a map with keys in reverse order more conveniently.

#include <iostream>
#include <map>

int main()
{
    // Create a map with the default greater comparator (C++17 and later)
    std::map<int, std::string, std::greater<>> mapObject;

    // Add elements to the map
    mapObject[3] = "Three";
    mapObject[1] = "One";
    mapObject[4] = "Four";
    mapObject[2] = "Two";

    // Iterate and print the elements of the map
    for (const auto &pair : mapObject)
    {
        std::cout << pair.first << " => " << pair.second << std::endl;
    }

    return 0;
}

Output:

4 => Four
3 => Three
2 => Two
1 => One

Summary

Today, we learned two methods to store keys in reverse order in a std::map in C++. The first method uses a custom comparator std::greater<int>, and the second, more modern approach, uses the std::greater<> transparent comparator introduced in C++17.

Ritika Ohri

Hi, I am Ritika Ohri, founder of this blog. I craft comprehensive programming tutorials and also manage a YouTube channel. You can also connect with me on Linkedin.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.