How to Create a Map of Arrays in C++?
Last Updated :
06 May, 2024
In C++, the std::map is a container that stores elements in a key-value pair, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a map of arrays in C++.
Example:
Input:
arr1 = {1, 2, 3};
arr2 = {4, 5, 6};
arr3 = {7, 8, 9};
Output:
Map of Array: { {“first”, {1, 2, 3}},
{“second”, {4, 5, 6}},
{“third”, {7, 8, 9}} }
Map of Arrays in C++
To create a map of arrays in C++, we can use the std::array container provided by the STL library. The std::array is a container that encapsulates fixed-size arrays. We can use them to create a map of arrays.
Syntax to Create a Map of Arrays in C++
map< keytype, array<datatype, size>> map_name;
Here,
keytype
denotes the type of key you want to use in the map.datatype
denotes the type of data stored in the array.size
denotes the size of the array.map_name
is the name of the map of arrays.
C++ Program to Create Map of Arrays
The below program demonstrates how we can create a map of arrays in C++ STL.
C++
// C++ Program to illustrate how to create a map of arrays
#include <array>
#include <iostream>
#include <map>
using namespace std;
int main()
{
// Creating a map of arrays
map<string, array<int, 3> > mapArr;
// Inserting elements into the map
mapArr["first"] = { 1, 2, 3 };
mapArr["second"] = { 4, 5, 6 };
// Printing the map of arrays
cout << "Elements in a Map of Arrays are: " << endl;
for (auto& it : mapArr) {
cout << it.first << " -> ";
for (int i : it.second) {
cout << i << " ";
}
cout << endl;
}
return 0;
}
OutputElements in a Map of Arrays are:
first -> 1 2 3
second -> 4 5 6
Time Complexity: O(N log N)
, where N
is the number of arrays.
Auxilliary Space: O(N * M), where M is the average size of the arrays.
Similar Reads
How to Create a Multimap of Arrays in C++? In C++, a multimap is similar to a map with the addition that multiple elements can have the same keys. Also, it is NOT required that the key-value and mapped value pair have to be unique in this case. In this article, we will learn how to create a multimap of arrays in C++ STL. Example Input: myArr
2 min read
How to Create Array of Arrays in C++ Arrays are basic C++ data structures that allow users to store the same data type in memory sequentially. To manage more complicated data structures, you may sometimes need to build an array of arrays, often called a 2D array or a matrix. In this article, we will learn how to create an array of arra
3 min read
How to Create a Set of Arrays in C++? In C++, the set container represents a collection of unique, sorted elements, and an array is a collection of items stored at contiguous memory locations. In this article, we will learn about how to create a set of arrays in C++. Set of Arrays in C++A set of arrays refers to a collection of arrays w
2 min read
How to Create a Stack of Arrays in C++? In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a stack of an array in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; ar
2 min read
How to Create a Vector of Arrays in C++? In C++, an array is a collection of elements of a single type while vectors are dynamic arrays as they can change their size during the insertion and deletion of elements. In this article, we will learn how to create a vector of arrays in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; arr3
2 min read