Open In App

Different Ways to Initialize an unordered_map in C++

Last Updated : 19 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

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




// C++ program to implement
// the above approach
#include <iostream>
#include <unordered_map>
using namespace std;
 
// Driver code
int main()
{
    // Initialize unordered_map
    // using default constructor
    unordered_map<string,string>New_map;
   
    // Adding key-value pairs using
    // subscript([]) and assignment(=)
    // operators
    New_map["Ground"] = "Grass";
    New_map["Floor"] = "Cement";
    New_map["Table"] = "Wood";
   
    // Traverse through the unordered_map
    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++




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




// 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]));
   
    // Adding these key-value pairs using
    // the pairs stored in the array of pairs   
    unordered_map<string,
                  string>New_map(old_arr,
                                 old_arr + n);
   
    // Traverse through the unordered_map
    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++




// C++ program to implement
// the above approach
#include <iostream>
#include <unordered_map>
using namespace std;
 
// Driver code
int main()
{
    // Initialize an unordered_map using
    // default constructor
    unordered_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   
    unordered_map<string, string>New_map;
    New_map.insert(old_map.begin(),
                   old_map.end());
   
    // 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

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




// C++ program to implement
// the above approach
#include <iostream>
#include <unordered_map>
using namespace std;
 
// Driver code
int main()
{
    // Initialize an unordered_map
    // using default constructor
    unordered_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
    unordered_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

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




// C++ program to implement
// the above approach
#include <iostream>
#include <unordered_map>
using namespace std;
 
// Driver code
int main()
{
    // Initialize an unordered_map
    // using default constructor
    unordered_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   
    unordered_map<string,
                  string>New_map(old_map.begin(),
                                 old_map.end());
   
    // 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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads