Monday, November 18, 2024
Google search engine
HomeData Modelling & AIArray of unordered maps in C++ with Examples

Array of unordered maps in C++ with Examples

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 maps
dataType1: The dataType for the key
dataType2: The dataType for the value

Array of unordered map

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 array
void 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 code
int 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;
}


Output

The unordered map elements stored at the index 0:
Key Value
25 0
20 1
10 1
15 0

The unordered map elements stored at the index 1:
Key Value
45 0
40 1
30 1
35 0

The unordered map elements stored at the index 2:
Key Value
65 0
60 1
50 1
55 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 index
void print(unordered_map<string, 
           bool>* myContainer, int n)
{
    for (int i = 0; i < n; i++) 
    {
        print(myContainer[i], i);
    }
}
  
// Driver code
int 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;
}


Output

The unordered map elements stored at the index 0:
Key Value
Solo 0
Java 1
Code 1
HTML 0

The unordered map elements stored at the index 1:
Key Value
Lab 0
C++ 1
PHP 1
CSS 0

The unordered map elements stored at the index 2:
Key Value
Fizzy 1
Pizza 0
Swift 1
Cobol 0

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments