How to Create a Stack of Unordered_Multimap in C++?
Last Updated :
19 Mar, 2024
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”} };
myMultimap2 = { {2, “JavaScript”}, {3, "R" }};
Output:
myStack: [ { {1, “C++”}, {2, “Java”}, {1, “Python”} },
{ {2, “JavaScript”} } ]
Stack of Unordered_Multimaps in C++
To create a std::stack of std::unordered_multimaps in C++, we have to define the type of stack elements as an unordered_multimap in the template definition.
Syntax to Declare Stack of Unordered_Multimap
stack < unordered_multimap <keyType, valueType> > myStack;
Here,
key_type
is the type of key stored in the unordered_multimapvalue_type
is the type of value stored in the unordered_multimap.myStack
is the name of the stack of unordered_multimap.
C++ Program to Create Stack of Unordered_Multimap
The below program demonstrates how we can create and use a stack of unordered_multimap in C++ STL.
C++
// C++ Program to illustrate how to create a stack of
// unordered_multimaps
#include <iostream>
#include <stack>
#include <unordered_map>
using namespace std;
int main()
{
// Initialize an unordered_multimap with some entries
unordered_multimap<int, string> myUnorderedMultimap1
= { { 1, "C++" }, { 2, "Java" }, { 1, "Python" } };
unordered_multimap<int, string> myUnorderedMultimap2
= { { 2, "JavaScript" }, { 3, "R" } };
;
// Create a stack of unordered_multimaps
stack<unordered_multimap<int, string> > myStack;
myStack.push(myUnorderedMultimap1);
myStack.push(myUnorderedMultimap2);
// Print unordered_multimap in the stack
cout << "myStack:" << endl << "[";
while (!myStack.empty()) {
auto ele = myStack.top();
cout << " { ";
for (auto& pair : ele) {
cout << "{" << pair.first << ", " << pair.second
<< "}, ";
}
cout << " } ";
myStack.pop();
}
cout << " ]";
return 0;
}
OutputmyStack:
[ { {3, R}, {2, JavaScript}, } { {1, Python}, {1, C++}, {2, Java}, } ]
Time Complexity: O(N), here N is the total number of elements across all unordered_multimaps.
Auxiliary Space: O(N)
Similar Reads
How to Create a Stack of Unordered_Map in C++? 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.
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 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
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 Unordered Multiset of Tuples in C++? In C++, an unordered multiset is a container that stores elements in no particular order, allowing fast retrieval of individual elements based on their value, much like unordered set containers, but allowing different elements to have equivalent values. In this article, we will learn how to create a
2 min read