Array of unordered maps in C++ with Examples - GeeksforGeeks

27 Dec, 2021
What is an array?
An array in any programming language is a data structure that is used to store elements or data items of similar data types at contiguous memory locations and elements can be accessed randomly using indices of an array. Arrays are efficient when we want to store a large number of elements that too of similar data types.
What is Unordered Map?
Unordered_map is an associated container that stores elements formed by the combination of key-value and a mapped value. The key value is used to uniquely identify the element and the mapped value is the content associated with the key. Both key and value can be of any type predefined or user-defined. Elements in an unordered map are not arranged in any particular order.  Internally, an unordered map is implemented using a Hash Table.
Functions used with unordered map:
at(): This function in C++ unordered_map returns the reference to the value with the element as key k.
begin(): Returns an iterator pointing to the first element in the container in the unordered_map container
end(): Returns an iterator pointing to the position past the last element in the container in the unordered_map container
This article focuses on how the array of unordered maps can be used in C++. An array of unordered maps can be quite useful while designing complex data structures.
Array of unordered maps
C++ allows us a facility to create an array of unordered maps. An array of unordered maps is an array in which each element is a map on its own.
Syntax:
unordered_map<<dataType1, dataType2>> myContainer[N];
Here,
N: The size of the array of unordered mapsdataType1: The dataType for the keydataType2: The dataType for the value
Example 1: Below is the C++ program to implement the approach:
C++
// C++ program to demonstrate the// working of array of unordered maps in C++#include <bits/stdc++.h>using namespace std;  // Function to print unordered map elements// specified at the index, "index"void print(unordered_map<int, bool>& myMap,           int index){    cout << "The unordered map elements stored " <<             "at the index " << index << ": \n";    cout << "Key      Value\n";      // Each element of the map is a pair on     // its own    for (auto pr : myMap)     {        // Each element of the map is a pair         // on its own        cout << pr.first << "         " <<                 pr.second << '\n';    }    cout << '\n';}  // Function to iterate over all the arrayvoid print(unordered_map<int,            bool>* myContainer, int n){    // Iterating over myContainer elements    // Each element is a map on its own    for (int i = 0; i < n; i++)     {        print(myContainer[i], i);    }}  // Driver codeint main(){    // Declaring an array of unordered maps    // In unordered map Key is of type int    // Value is of type bool    unordered_map<int, bool> myContainer[3];      // Mapping values to the unordered map     // stored at the index 0    myContainer[0][10] = true;    myContainer[0][15] = false;    myContainer[0][20] = true;    myContainer[0][25] = false;      // Mapping values to the unordered map     // stored at the index 1    myContainer[1][30] = true;    myContainer[1][35] = false;    myContainer[1][40] = true;    myContainer[1][45] = false;      // Mapping values to the unordered map     // stored at the index 2    myContainer[2][50] = true;    myContainer[2][55] = false;    myContainer[2][60] = true;    myContainer[2][65] = false;      // Calling print function to iterate    // over myContainer elements    print(myContainer, 3);      return 0;}
The unordered map elements stored at the index 0:Key Value25 020 110 115 0
The unordered map elements stored at the index 1:Key Value45 040 130 135 0
The unordered map elements stored at the index 2:Key Value65 060 150 155 0
Example 2: Below is the C++ program to implement the approach:
C++
// C++ program to demonstrate the// working of array of maps in C++#include <bits/stdc++.h>using namespace std;  // Function to print unordered map elements // specified at the index, "index"void print(unordered_map<string,            bool>& myMap, int index){    cout << "The unordered map elements stored " <<             "at the index " << index << ": \n";    cout << "Key      Value\n";      // Each element of the unordered map is     // a pair on its own    for (auto pr : myMap)     {        cout << pr.first << "      " <<                 pr.second << '\n';    }    cout << '\n';}  // Function to iterate over the unordered // map corresponding to an indexvoid print(unordered_map<string,            bool>* myContainer, int n){    for (int i = 0; i < n; i++)     {        print(myContainer[i], i);    }}  // Driver codeint main(){    // Declaring an array of unordered maps    // In unordered map Key is of type string    // Value is of type bool    unordered_map<string, bool> myContainer[3];      // Mapping values to the unordered map     // stored at the index 0    myContainer[0]["Code"] = true;    myContainer[0]["HTML"] = false;    myContainer[0]["Java"] = true;    myContainer[0]["Solo"] = false;      // Mapping values to the unordered map     // stored at the index 1    myContainer[1]["PHP"] = true;    myContainer[1]["CSS"] = false;    myContainer[1]["C++"] = true;    myContainer[1]["Lab"] = false;      // Mapping values to the unordered map     // stored at the index 2    myContainer[2]["Swift"] = true;    myContainer[2]["Cobol"] = false;    myContainer[2]["Fizzy"] = true;    myContainer[2]["Pizza"] = false;      // Calling print function to print     // myContainer elements    print(myContainer, 3);      return 0;}
The unordered map elements stored at the index 0:Key ValueSolo 0Java 1Code 1HTML 0
The unordered map elements stored at the index 1:Key ValueLab 0C++ 1PHP 1CSS 0
The unordered map elements stored at the index 2:Key ValueFizzy 1Pizza 0Swift 1Cobol 0
Arrays
cpp-array
cpp-unordered_map
STL
Arrays
C++
Arrays
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Program to find sum of elements in a given array
Building Heap from Array
Window Sliding Technique
Reversal algorithm for array rotation
1's and 2's complement of a Binary Number
Vector in C++ STL
Inheritance in C++
Initialize a vector in C++ (6 different ways)
Multidimensional Arrays in C / C++
getline (string) in C++