Open In App

Array of unordered maps in C++ with Examples

Last Updated : 27 Dec, 2021
Comments
Improve
Suggest changes
1 Like
Like
Report

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:


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:


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

Next Article
Practice Tags :

Similar Reads