Skip to content
Related Articles

Related Articles

Different Ways to Initialize a Map in C++

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 13 Apr, 2022

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++.

  1. Initialization using assignment and subscript operator
  2. Initialization using an initializer list
  3. Initialization using an array of pairs
  4. Initialization from another map using the map.insert() method
  5. Initialization from another map using the copy constructor
  6. 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++




// C++ program to implement the 
// above approach
#include <iostream>
#include <map>
using namespace std;
  
// Driver code
int main() 
{
   // Initialize map using 
   // default constructor
   map<string, string>New_Map;
   
   // Keep on adding key-value pairs 
   // using subscript([]) and 
   // assignment(=) operators
   New_Map["Ground"] = "Grass";
   New_Map["Floor"] = "Cement";
   New_Map["Table"] = "Wood";
   
   // Traverse through the map
   for(auto x: New_Map)
   {
      cout << x.first << "->" << 
              x.second <<endl;
   }
   return 0;
}

Output

Floor->Cement
Ground->Grass
Table->Wood

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++




// C++ program to implement 
// the above approach
#include <iostream>
#include <map>
using namespace std;
  
// Driver code
int main() 
{
   // Initialize map using 
   // default constructor
   map<string, string>New_Map;
   
   // Adding key-value pairs 
   // using Initializer list
   New_Map = {{"Ground", "Grass"}, 
              {"Floor", "Cement"}, 
              {"Table", "Wood"}};
     
   // Traverse through the map
   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++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Driver code
int main() 
{
   // Initialize an array of pair 
   // of strings
   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]));
   
   // Add these key-value pairs using 
   // the pairs stored in the array of pairs   
   map<string, string>New_Map(old_arr, 
                              old_arr + n);
   
   // Traverse through the map
   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++




// C++ program to implement 
// the above approach
#include <iostream>
#include <map>
using namespace std;
  
// Driver code
int main() 
{
   // Initialize an map using 
   // default constructor
   map<string, string>old_map;
   
   // Adding key-value pairs using 
   // subscript([]) and assignment(=) operators
   old_map["Ground"] = "Grass";
   old_map["Floor"] = "Cement";
   old_map["Table"] = "Wood";
   
   
   // Create a new_map where contents 
   // of the previous map will be 
   // copied using copy constructor 
   // and iterator provided by the map   
   map<string, string>New_Map;
   New_Map.insert(old_map.begin(),
                  old_map.end());
   
   // Traverse through the map
   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++




// C++ program to implement 
// the above approach
#include <iostream>
#include <map>
using namespace std;
  
// Driver code
int main() 
{
   // Initialize a map using 
   // default constructor
   map<string, string>old_map;
   
   // Adding key-value pairs using 
   // subscript([]) and assignment(=) 
   // operators
   old_map["Ground"] = "Grass";
   old_map["Floor"] = "Cement";
   old_map["Table"] = "Wood";
   
   // Create a new_map where contents 
   // of the previous map will be copied 
   // using copy constructor   
   map<string, string>New_Map(old_map);
   
   // Traverse through the unordered_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++




// C++ program to implement 
// the above approach
#include <iostream>
#include <map>
using namespace std;
  
// Driver code
int main() 
{
   // Initialize a map using 
   // default constructor
   map<string,string>old_map;
   
   // Adding key-value pairs using 
   // subscript([]) and assignment(=) 
   // operators
   old_map["Ground"] = "Grass";
   old_map["Floor"] = "Cement";
   old_map["Table"] = "Wood";
   
   // Create a new_map where a range 
   // of key-value pairs are stored 
   // from old_map
   map<string, string>New_Map(old_map.begin(), 
                              old_map.end());
   
   // Traverse through the map
   for(auto x: New_Map)
   {
      cout << x.first << "->" << 
              x.second <<endl;
   }
   return 0;
}

Output

Floor->Cement
Ground->Grass
Table->Wood


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!