Vector of Unordered Maps in C++ with Examples - GeeksforGeeks

29 Nov, 2021
Vector:
Vector: It is the same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators.
Some of the functions used with vector:
begin(): It returns an iterator pointing to the first element in the vector
end(): It returns an iterator pointing to the theoretical element that follows the last element in the vector
size(): It returns the number of elements in the vector.
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 Hash Table.
Some of the 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 vector of unordered maps can be used in C++. Vector of unordered maps can be quite useful while designing complex data structures.
Below is the implementation using a vector of unordered maps:
 Example 1:
C++
// C++ program to illustrate the working// of vector of unordered maps#include <bits/stdc++.h>using namespace std;  // Function to print vector elementsvoid print(vector<unordered_map<int, int> >& vect){      cout << "vector : \n";    for (int i = 0; i < (int)vect.size(); i++) {          // Each element of the vector is a unordered map        unordered_map<int, int> unorderedMap = vect[i];          cout << "unordered map : ";        cout << "[  ";          // Print unordered map elements        for (auto it = unorderedMap.begin();             it != unorderedMap.end(); it++) {            cout << it->first << ':' << it->second << "  ";        }          cout << "]\n";    }}  // Driver Codeint main(){      // Declaring a vector of unordered maps    vector<unordered_map<int, int> > vect;      // Declaring a unordered map    unordered_map<int, int> unorderedMap1;      // Hashing values    unorderedMap1[2] = 1;    unorderedMap1[4] = 7;    unorderedMap1[6] = 10;      // Push back the unordered map in the vector    vect.push_back(unorderedMap1);      // Declaring another unordered map    unordered_map<int, int> unorderedMap2;      // Hashing values    unorderedMap2[14] = 11;    unorderedMap2[15] = 21;    unorderedMap2[6] = 34;      // Push back the unordered map in the vector    vect.push_back(unorderedMap2);      // Declaring another unordered map    unordered_map<int, int> unorderedMap3;      // Hashing values    unorderedMap3[7] = 277;    unorderedMap3[18] = 188;    unorderedMap3[9] = 399;    // Push back the unordered map in the vector    vect.push_back(unorderedMap3);      // Declaring another unordered map    unordered_map<int, int> unorderedMap4;      // Hashing values    unorderedMap4[121] = 88;    unorderedMap4[97] = 99;    unorderedMap4[197] = 199;      // Push back the unordered map in the vector    vect.push_back(unorderedMap4);      print(vect);    return 0;}
vector : 
unordered map : [  6:10  2:1  4:7  ]
unordered map : [  6:34  14:11  15:21  ]
unordered map : [  9:399  7:277  18:188  ]
unordered map : [  197:199  121:88  97:99  ]
Example 2:
C++
// C++ program to illustrate the working// of vector of unordered maps#include <bits/stdc++.h>using namespace std;  // Function to print vector elementsvoid print(vector<unordered_map<int, string> >& vect){      cout << "vector : \n";    for (int i = 0; i < (int)vect.size(); i++) {        // Each element of the vector is a unordered map        unordered_map<int, string> unorderedMap = vect[i];          cout << "unordered map : ";        cout << "[  ";          // Print unordered map elements        for (auto it = unorderedMap.begin();             it != unorderedMap.end(); it++) {            cout << it->first << ':' << it->second << "  ";        }          cout << "]\n";    }}  // Driver Codeint main(){      // Declaring a vector of unordered maps    vector<unordered_map<int, string> > vect;      // Declaring a unordered map    unordered_map<int, string> unorderedMap1;      // Hashing values    unorderedMap1[11] = "Geeks";    unorderedMap1[23] = "for";    unorderedMap1[32] = "Geeks";      // Push back the unordered map in the vector    vect.push_back(unorderedMap1);      // Declaring another unordered map    unordered_map<int, string> unorderedMap2;      // Hashing values    unorderedMap2[12] = "Python";    unorderedMap2[32] = "Java";    unorderedMap2[73] = "C++";      // Push back the unordered map in the vector    vect.push_back(unorderedMap2);      // Declaring another unordered map    unordered_map<int, string> unorderedMap3;      // Hashing values    unorderedMap3[11] = "PHP";    unorderedMap3[2] = "C#";    unorderedMap3[35] = "Assembly";    // Push back the unordered map in the vector    vect.push_back(unorderedMap3);      // Declaring another unordered map    unordered_map<int, string> unorderedMap4;      // Hashing values    unorderedMap4[14] = "C";    unorderedMap4[27] = "Javascript";    unorderedMap4[54] = "Swift";      // Push back the unordered map in the vector    vect.push_back(unorderedMap4);      print(vect);    return 0;}
vector : 
unordered map : [  32:Geeks  11:Geeks  23:for  ]
unordered map : [  73:C++  12:Python  32:Java  ]
unordered map : [  35:Assembly  11:PHP  2:C#  ]
unordered map : [  54:Swift  14:C  27:Javascript  ]
Example 3:
C++
// C++ program to illustrate the working// of vector of unordered maps#include <bits/stdc++.h>using namespace std;  // Function to print vector elementsvoid print(vector<unordered_map<int, char> >& vect){      cout << "vector : \n";    for (int i = 0; i < (int)vect.size(); i++) {        // Each element of the vector is a unordered map        unordered_map<int, char> unorderedMap = vect[i];          cout << "unordered map : ";        cout << "[  ";          // Print unordered map elements        for (auto it = unorderedMap.begin();             it != unorderedMap.end(); it++) {            cout << it->first << ':' << it->second << "  ";        }          cout << "]\n";    }}  // Driver Codeint main(){      // Declaring a vector of unordered maps    vector<unordered_map<int, char> > vect;      // Declaring a unordered map    unordered_map<int, char> unorderedMap1;      // Hashing values    unorderedMap1[2] = 'G';    unorderedMap1[7] = 'e';    unorderedMap1[12] = 'e';    unorderedMap1[14] = 'k';    unorderedMap1[21] = 's';      // Push back the unordered map in the vector    vect.push_back(unorderedMap1);      // Declaring another unordered map    unordered_map<int, char> unorderedMap2;      // Hashing values    unorderedMap2[13] = 'J';    unorderedMap2[15] = 'a';    unorderedMap2[24] = 'v';    unorderedMap2[27] = 'a';      // Push back the unordered map in the vector    vect.push_back(unorderedMap2);      // Declaring another unordered map    unordered_map<int, char> unorderedMap3;      // Hashing values    unorderedMap3[33] = 'P';    unorderedMap3[37] = 'y';    unorderedMap3[41] = 't';    unorderedMap3[19] = 'h';    unorderedMap3[43] = 'o';    unorderedMap3[53] = 'o';      // Push back the unordered map in the vector    vect.push_back(unorderedMap3);      // Declaring another unordered map    unordered_map<int, char> unorderedMap4;      // Hashing values    unorderedMap4[15] = 's';    unorderedMap4[53] = 'w';    unorderedMap4[16] = 'i';    unorderedMap4[23] = 'f';    unorderedMap4[27] = 't';      // Push back the unordered map in the vector    vect.push_back(unorderedMap4);      print(vect);    return 0;}
vector : 
unordered map : [  12:e  2:G  21:s  14:k  7:e  ]
unordered map : [  24:v  27:a  13:J  15:a  ]
unordered map : [  53:o  43:o  41:t  19:h  33:P  37:y  ]
unordered map : [  27:t  23:f  16:i  15:s  53:w  ]
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Friend class and function in C++
Sorting a vector in C++
std::string class in C++
Pair in C++ Standard Template Library (STL)
Inline Functions in C++
Array of Strings in C++ (5 Different Ways to Create)
Queue in C++ Standard Template Library (STL)
Convert string to char array in C++