Initialize a std::map or std::unordered_map in C++
This post will discuss how to initialize a map in C++.
There are several approaches to initialize a std::map or std::unordered_map in C++, as shown below:
1. Using Initializer List
In C++11 and above, we can use the initializer lists '{...}' to initialize a map container.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <unordered_map> int main() { std::unordered_map<int, std::string> m = { {1, "one"}, {2, "two"}, {3, "three"} }; for (auto const &pair: m) { std::cout << "{" << pair.first << " -> " << pair.second << "}\n"; } return 0; } |
Output:
{3 -> three}
{1 -> one}
{2 -> two}
Instead of using brackets, we can give something meaningful to construct pairs like specifying their fully qualified name or using std::make_pair.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <unordered_map> int main() { std::unordered_map<int, std::string> m = { std::pair<int, std::string> (1, "one"), std::pair<int, std::string> (2, "two"), std::pair<int, std::string> (3, "three") }; for (auto const &pair: m) { std::cout << "{" << pair.first << " -> " << pair.second << "}\n"; } return 0; } |
Output:
{3 -> three}
{1 -> one}
{2 -> two}
We can also pass a binary predicate with std::map, which takes two values of the same type and defines the ordering of the map’s keys. The predicate returns true if the first parameter appears before the second parameter and false otherwise.
|
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 |
#include <iostream> #include <map> // Binary predicate struct comp { template<typename T> bool operator() (const T &l, const T &r) const { return l > r; } }; int main() { // The keys are sorted according to the comparison object std::map<int, std::string, comp> m = { std::make_pair(1, "one"), std::make_pair(2, "two"), std::make_pair(3, "three") }; for (auto const &pair: m) { std::cout << "{" << pair.first << " -> " << pair.second << "}\n"; } return 0; } |
Output:
{3 -> three}
{2 -> two}
{1 -> one}
2. From array of pairs
We can use a range constructor to initialize the set from elements of an array of pairs or another container of pairs.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> #include <unordered_map> int main() { std::pair<int, std::string> arr[] = { std::make_pair(1, "one"), std::make_pair(2, "two"), std::make_pair(3, "three") }; int n = sizeof(arr) / sizeof(arr[0]); std::unordered_map<int, std::string> m(arr, arr + n); // or do // std::unordered_map<int, std::string> m(std::begin(arr), std::end(arr)); for (auto pair: m) { std::cout << "{" << pair.first << " -> " << pair.second << "}\n"; } return 0; } |
Output:
{3 -> three}
{1 -> one}
{2 -> two}
3. From another map
We can use a copy constructor to initialize a map from elements of another map.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <unordered_map> int main() { std::unordered_map<int, std::string> map = { std::make_pair(1, "one"), std::make_pair(2, "two"), std::make_pair(3, "three") }; std::unordered_map<int, std::string> m(map); for (auto const &pair: m) { std::cout << "{" << pair.first << " -> " << pair.second << "}\n"; } return 0; } |
Output:
{2 -> two}
{3 -> three}
{1 -> one}
4. Using Default Constructor
We can use the empty container constructor (or a default constructor) to construct an empty map (with no elements), as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <unordered_map> int main() { std::unordered_map<int, std::string> m; for (auto const &pair: m) { std::cout << "{" << pair.first << " -> " << pair.second << "}\n"; } return 0; } |
Output:
The standard output is empty
That’s all about initializing a std::map or std::unordered_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 :)