How to use std::pair as a key to std::map in C++
This post will discuss how to use std::pair as a key to std::map in C++. The std::pair in C++ binds together a pair of values of the same or different types, which can then be accessed through its first and second public members.
1. Using default order
We know that the third template parameter of std::map defaults to std::less, which will delegate to operator<. So, C++ expects operator< to be defined for the type used for map’s keys.
Since the operator< is already defined for pairs, we can initialize a std::map with std::pair as the key.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <iostream> #include <map> #include <utility> typedef std::pair<std::string, std::string> pair; int main() { std::map<pair, int> map = { { std::make_pair("C++", "C++14"), 2014 }, { std::make_pair("C++", "C++17"), 2017 }, { std::make_pair("Java", "Java 7"), 2011 }, { std::make_pair("Java", "Java 8"), 2014 }, { std::make_pair("C", "C11"), 2011 } }; for (const auto &entry: map) { auto key_pair = entry.first; std::cout << "{" << key_pair.first << "," << key_pair.second << "}, " << entry.second << std::endl; } return 0; } |
Output:
{C,C11}, 2011
{C++,C++14}, 2014
{C++,C++17}, 2017
{Java,Java 7}, 2011
{Java,Java 8}, 2014
2. Using a comparison object
We can also pass a comparison object as the third template parameter to std::map to override its default order. The comparison object takes two pair objects and defines the ordering of the map’s keys by comparing the first and second elements of both pairs. If it returns true, the first pair appears before the second pair, and vice versa.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include <iostream> #include <map> #include <utility> typedef std::pair<std::string, std::string> pair; struct comp { template<typename T> bool operator()(const T &l, const T &r) const { if (l.first == r.first) { return l.second > r.second; } return l.first < r.first; } }; int main() { std::map<pair,int, comp> map = { { std::make_pair("C++", "C++14"), 2014 }, { std::make_pair("C++", "C++17"), 2017 }, { std::make_pair("Java", "Java 7"), 2011 }, { std::make_pair("Java", "Java 8"), 2014 }, { std::make_pair("C", "C11"), 2011 } }; for (const auto &entry: map) { auto key_pair = entry.first; std::cout << "{" << key_pair.first << "," << key_pair.second << "}, " << entry.second << std::endl; } return 0; } |
Output:
{C,C11}, 2011
{C++,C++17}, 2017
{C++,C++14}, 2014
{Java,Java 8}, 2014
{Java,Java 7}, 2011
3. Specializing std::less function
There is another way to override the default order without using the comparison object. The idea is to specialize std::less in the std namespace. This can be done, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include <iostream> #include <map> #include <utility> typedef std::pair<std::string, std::string> pair; namespace std { template<typename T1, typename T2> struct less<std::pair<T1, T2>> { bool operator()(const std::pair<T1, T2> &l, const std::pair<T1, T2> &r) const { if (l.first == r.first) { return l.second > r.second; } return l.first > r.first; } }; } int main() { std::map<pair, int> map = { { std::make_pair("C++", "C++14"), 2014 }, { std::make_pair("C++", "C++17"), 2017 }, { std::make_pair("Java", "Java 7"), 2011 }, { std::make_pair("Java", "Java 8"), 2014 }, { std::make_pair("C", "C11"), 2011 } }; for (const auto &entry: map) { auto key_pair = entry.first; std::cout << "{" << key_pair.first << "," << key_pair.second << "}, " << entry.second << std::endl; } return 0; } |
Output:
{Java,Java 8}, 2014
{Java,Java 7}, 2011
{C++,C++17}, 2017
{C++,C++14}, 2014
{C,C11}, 2011
That’s all about using std::pair as a key to std::map in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)