How to Create a Stack of Unordered_Map in C++?
Last Updated :
18 Mar, 2024
In C++, the stack is a container that follows the LIFO(Last In First Out) rule where new elements are added from one end (top) and removed from that end only. An unordered_map is an associative container that stores elements formed by a combination of key-value pairs, where the key should be unique. In this article, we will learn how to create a stack of unordered_map in C++.
Example:
Input:
myMap1 = { {“apple”, 1}, {“banana”, 2} }
myMap2 = { {“orange”, 3}, {“mango”, 4} }
Output:
Stack of Unordered_Map: [ { {“orange”, 3}, {“mango”, 4} },
{ {“apple”, 1}, {“banana”, 2} } ]
Stack of Unordered_Map in C++
To create a stack of unordered_map in C++, we need to pass std::unordered_map as the template parameter in the declaration of the stack and then use the std::stack::push() function to insert the unordered_maps in the stack.
Syntax to Declare Stack of Unordered_Maps in C++
stack<unordered_map<key_type, value_type>> stack_name;
Here,
key_type
is the type of key stored in the unordered_map.value_type
is the type of value stored in the unordered_map.stack_name
is the name of the stack of unordered_map.
C++ Program to Create Stack of Unordered_Map
The below program demonstrates how we can create and use a stack of unordered_map in C++ STL.
C++
// C++ Program to illustrate how to create a stack of
// Unordered_Map
#include <iostream>
#include <stack>
#include <unordered_map>
using namespace std;
int main()
{
// Defining multiple unordered_maps
unordered_map<int, string> map1
= { { 1, "One" }, { 2, "Two" }, { 3, "Three" } };
unordered_map<int, string> map2
= { { 4, "Four" }, { 5, "Five" }, { 6, "Six" } };
// Creating a stack of unordered_maps
stack<unordered_map<int, string> > stackOfMaps;
// Pushing unordered_maps in the stack
stackOfMaps.push(map1);
stackOfMaps.push(map2);
// Printing elements from the stack of unordered_maps
cout << "Elements in the Stack of Unordered_Map:"
<< endl;
while (!stackOfMaps.empty()) {
unordered_map<int, string> currMap
= stackOfMaps.top();
stackOfMaps.pop();
for (auto& it : currMap) {
cout << "{" << it.first << ", " << it.second
<< "} ";
}
cout << endl;
}
return 0;
}
OutputElements in the Stack of Unordered_Map:
{6, Six} {4, Four} {5, Five}
{3, Three} {1, One} {2, Two}
Time Complexity: O(N), here N is the total number of unordered_maps.
Auxiliary Space: O(N * M), here M is the average number of elements in the unordered_map.
Similar Reads
How to Create a Stack of Unordered_Multimap in C++? In C++, an unordered_multimap is an associative container that contains key-value pairs allowing multiple elements with the same key. In this article, we will learn how to create a stack of unordered_multimaps in C++. Example: Input: myMultimap1 = { {1, âC++â}, {2, âJavaâ}, {1, âPythonâ} }; myMultim
2 min read
How to Create a Stack of Unordered_Multiset in C++? In C++, the stack is a container in which new elements are added from one end (top) and removed from that end only. In this article, we will learn how to create a stack of unordered_multiset in C++. Example: Input: mySet1 = { âappleâ, âbananaâ, âappleâ } mySet2 = { âorangeâ, âmangoâ, âorangeâ } Outp
2 min read
How to Create Stack of Unordered Set in C++? In C++, a stack is a container adapter that provides a Last-In-First-Out (LIFO) type of data structure, and an unordered set is a container that stores unique elements in no particular order. In this article, we will learn how to create a stack of unordered sets in C++. Creating a Stack of Unordered
2 min read
How to Create a Stack of Map in C++? In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::map is an associative container that stores key-value pairs. In this article, we will learn how to create a stack of a map in C++. Example:Input: myMap = { {1: âaâ}, {2: âbâ}, {3: âcâ} }; myMap = { {4
2 min read
How to Create a Unordered Multimap of Tuples in C++? In C++, an unordered multimap container stores key-value pairs in no particular order. Unlike a map, an unordered multimap allows multiple values to be associated with a single key. In this article, we will learn how to create an unordered multimap of tuples in C++. For Example, Input: myPair1 = { "
2 min read
How to Create a Stack of Multimap in C++? In C++, Stacks are a type of container adaptor with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. A multimap is a container that stores key-value pairs in an ordered manner. In this article, we will learn how to c
2 min read