Initialize unordered_map c++ – Different Ways to Initialize an Unordered_Map

Initialize unordered_map c++: The associated container unordered map stores elements formed by the combination of a key value and a mapped value. The element is uniquely identified by the key value, and the mapped value is the content associated with the key. Both the key and the value can be of any predefined or user-defined type.
Internally, unordered map is implemented using a Hash Table; the keys provided to map are hashed into hash table indices, so the performance of the data structure is heavily dependent on the hash function; however, the cost of searching, inserting, and deleting from a hash table is typically O(1) .

Initialize an Unordered_Map

Unordered_map tutorial: There are several ways to initialize unordered map some of them are:

Method #1: Using initializer_list

C++ initialize unordered_map: Unordered map has a overloaded constructor that takes an initializer list as an argument and can initialise an unordered map.

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    // creating new unordered map using initializer list
    unordered_map<std::string, int> stringmap(
        { { "Hello", 400 },
          { "This", 100 },
          { "is", 200 },
          { "BTechGeeks", 300 } });
    // printing the unordered map
    for (auto i : stringmap)
        cout << i.first << " " << i.second << endl;
    return 0;
}

Output:

BTechGeeks 300
This 100
is 200
Hello 400

Method #2:Using  assignment operator and subscript operator

Initialize unordered_map: We can initialize unordered_map using assignment operator(“=”) and subscript operator.

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    // creating new unordered map using initializer list
    unordered_map<std::string, int> stringmap;
    // initialzing using assignment and subscript operator
    stringmap["Hello"] = 400;
    stringmap["This"] = 100;
    stringmap["is"] = 200;
    stringmap["BTechGeeks"] = 300;

    // printing the unordered map
    for (auto i : stringmap)
        cout << i.first << " " << i.second << endl;
    return 0;
}

Output:

BTechGeeks 300
Hello 400
is 200
This 100

Method #3:Using another unordered_map

Initialize unordered_map c++: We can also use an existing unordered map to initialise a new unordered map.

unordered_map<std::string, int> newmap(map1);

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    // creating new unordered map using initializer list
    unordered_map<std::string, int> stringmap;
    // initialzing using assignment and subscript operator
    stringmap["Hello"] = 400;
    stringmap["This"] = 100;
    stringmap["is"] = 200;
    stringmap["BTechGeeks"] = 300;

    // intializing another unordered map from previous
    // unordered map
    unordered_map<std::string, int> newmap(stringmap);
    // printing the new unordered map
    for (auto i : newmap)
        cout << i.first << " " << i.second << endl;
    return 0;
}

Output:

BTechGeeks 300
Hello 400
is 200
This 100

Related Programs: