unordered_map is an associated container available in the C++ Standard Template Library(STL) that is used to store key-value pairs. It internally uses the working principle of a hashmap to store key-value pairs.
Different ways to initialize an unordered_map in C++:
- Initialization using assignment and subscript operator
- Initialization using an initializer list
- Initialization using an array of pairs
- Initialization from another map using unordered_map.insert() method
- Initialization from another map using the copy constructor
- Initialization through a range
1. Initialization Using Assignment and Subscript Operator
One of the simplest ways of initializing an unordered_map is to use the assignment(=) and the subscript([]) operators as shown below:
Syntax:
unordered_map<string,string>New_map;
New_map[“5”] = “6”;
Here,
- [] is the subscript operator
- = is the assignment operator
Below is the C++ program to implement the above approach:
C++
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
unordered_map<string,string>New_map;
New_map["Ground"] = "Grass";
New_map["Floor"] = "Cement";
New_map["Table"] = "Wood";
for(auto x: New_map)
{
cout << x.first << "->" <<
x.second <<endl;
}
return 0;
}
|
Output
Table->Wood
Ground->Grass
Floor->Cement
2. Initialization using an Initializer List
Another way of initializing an unordered_map is to use a predefined list of key-value pairs.
Syntax:
unordered_map<string,string>New_map = {{key1, value1}, {key2, value2}, {key3, value3}};
Below is the C++ program to implement the above approach:
C++
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
unordered_map<string, string>New_map;
New_map = {{"Ground", "Grass"},
{"Floor", "Cement"},
{"Table", "Wood"}};
for(auto x: New_map)
{
cout << x.first << "->" <<
x.second <<endl;
}
return 0;
}
|
Output
Table->Wood
Floor->Cement
Ground->Grass
3. Initialization Using an Array of Pairs
As unordered_map stores key-value pairs, one can store the key-values using the array of pairs of the same type.
Syntax:
unordered_map<string, string>New_map(old_arr, old_arr + n);
Here, old_arr is the array of pairs from which contents will be copied into the new_map.
In the case of the vector of pairs, use the inbuilt iterators to copy contents from the vector of pairs into the new unordered_map.
unordered_map<int, int>New_map(old_vector.begin(), old_vector.end());
Here, old_vector is the vector of pairs from which contents will be copied into the new_map.
Below is the C++ program to implement the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
pair<string,string>old_arr[] =
{
make_pair("Ground", "Grass"),
make_pair("Floor", "Cement"),
make_pair("Table", "Wood")
};
int n = (sizeof(old_arr) /
sizeof(old_arr[0]));
unordered_map<string,
string>New_map(old_arr,
old_arr + n);
for(auto x: New_map)
{
cout << x.first << "->" <<
x.second <<endl;
}
return 0;
}
|
Output
Table->Wood
Floor->Cement
Ground->Grass
4. Initialization From Another Map Using unordered_map.insert() Method
A standard way of copying elements from a map to an existing, old map in C++ is using the unordered_map.insert member function as shown below :
Syntax:
unordered_map<string, string> New_map;
New_map.insert(old_map.begin(), old_map.end());
Here, old_map is the map from which contents will be copied into the new_map.
Below is the C++ program to implement the above approach:
C++
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
unordered_map<string,string>old_map;
old_map["Ground"] = "Grass";
old_map["Floor"] = "Cement";
old_map["Table"] = "Wood";
unordered_map<string, string>New_map;
New_map.insert(old_map.begin(),
old_map.end());
for(auto x: New_map)
{
cout << x.first << "->" <<
x.second <<endl;
}
return 0;
}
|
Output
Floor->Cement
Ground->Grass
Table->Wood
5. Initialization From Another Map Using the Copy Constructor
One way to initialize a unordered_map is to copy contents from another map one after another by using the copy constructor.
Syntax:
unordered_map<string, string>New_map(old_map);
Here, old_map is the map from which contents will be copied into the new_map.
Below is the C++ program to implement the above approach:
C++
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
unordered_map<string,string>old_map;
old_map["Ground"] = "Grass";
old_map["Floor"] = "Cement";
old_map["Table"] = "Wood";
unordered_map<string, string>New_map(old_map);
for(auto x: New_map)
{
cout << x.first << "->" <<
x.second <<endl;
}
return 0;
}
|
Output
Table->Wood
Ground->Grass
Floor->Cement
6. Initialization Through a Range
Another way of initializing an unordered_map is to initialize it through a range of key-value pairs.
Syntax:
unordered_map<string,string>New_map(old_map.begin(),old_map.end());
Here, instead of using another map, we store any range of key-value pairs.
Below is the C++ program to implement the above approach:
C++
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
unordered_map<string, string>old_map;
old_map["Ground"] = "Grass";
old_map["Floor"] = "Cement";
old_map["Table"] = "Wood";
unordered_map<string,
string>New_map(old_map.begin(),
old_map.end());
for(auto x: New_map)
{
cout << x.first << "->" <<
x.second <<endl;
}
return 0;
}
|
Output
Floor->Cement
Ground->Grass
Table->Wood
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!
Last Updated :
19 Apr, 2022
Like Article
Save Article