Different Ways to Initialize a Map in C++
Last Updated :
30 Mar, 2023
Map is an associative container available in the C++ Standard Template Library(STL) that is used to store key-value pairs. Let’s see the different ways to initialize a 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 the 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 a map is to use the assignment(=) and the subscript([]) operators as shown below:
Syntax:
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 <map>
using namespace std;
int main()
{
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
Floor->Cement
Ground->Grass
Table->Wood
Time complexity : O(NlogN) for insertion and O(N) for traversal, where N is the number of elements in the map
Auxiliary space : O(N) to store the key-value pairs in the map
2. Initialization using an Initializer list
Another way of initializing a map is to use a predefined list of key-value pairs.
Syntax :
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 <map>
using namespace std;
int main()
{
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
Floor->Cement
Ground->Grass
Table->Wood
3. Initialization Using an Array of Pairs
The map stores key-value pairs, one can store the key values using the array of pairs of the same type.
Syntax:
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, one can use the inbuilt iterators to copy contents from the vector of pairs Into the new map.
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]));
map<string, string>New_Map(old_arr,
old_arr + n);
for ( auto x: New_Map)
{
cout << x.first << "->" <<
x.second <<endl;
}
return 0;
}
|
Output
Floor->Cement
Ground->Grass
Table->Wood
4. Initialization From Another Map Using map.insert() Method
A standard way of copying elements from a map to an existing old map in C++ is using the map.insert member function.
Syntax:
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 <map>
using namespace std;
int main()
{
map<string, string>old_map;
old_map[ "Ground" ] = "Grass" ;
old_map[ "Floor" ] = "Cement" ;
old_map[ "Table" ] = "Wood" ;
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;
}
}
|
Output
Floor->Cement
Ground->Grass
Table->Wood
5. Initialization From Another Map Using a Copy Constructor
One way to initialize a map is to copy contents from another map one after another by using the copy constructor.
Syntax:
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 <map>
using namespace std;
int main()
{
map<string, string>old_map;
old_map[ "Ground" ] = "Grass" ;
old_map[ "Floor" ] = "Cement" ;
old_map[ "Table" ] = "Wood" ;
map<string, string>New_Map(old_map);
for ( auto x: New_Map)
{
cout << x.first << "->" <<
x.second <<endl;
}
return 0;
}
|
Output
Floor->Cement
Ground->Grass
Table->Wood
6. Initialization Through a Range
Another way of initializing a map is to initialize it through a range of key-value pairs.
Syntax:
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 <map>
using namespace std;
int main()
{
map<string,string>old_map;
old_map[ "Ground" ] = "Grass" ;
old_map[ "Floor" ] = "Cement" ;
old_map[ "Table" ] = "Wood" ;
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
Share your thoughts in the comments
Please Login to comment...