How to Create a Stack of Multimap in C++?
Last Updated :
06 Mar, 2024
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 create a stack of multimaps in C++.
Example:
Input:
myMultimap1 = { {1, “C++”}, {2, “Java”}, {1, “Python”} };
myMultimap2 = { {2, “JavaScript”} };
Output:
myStack: [ { {1, “C++”}, {2, “Java”}, {1, “Python”} },
{ {2, “JavaScript”} } ]
Stack of Multimaps in C++
To create a stack of multimaps in C++, we have to define the type of stack elements as a multimap in the template definition. The stack can store any type of data, including complex data types like multimaps.
Syntax to Declare Stack of Multimap
stack < multimap <keyType, valueType> myStack;
C++ Program to Create a Stack of Multimaps
C++
// C++ Program to illustrate how to create a stack of
// multimaps
#include <iostream>
#include <map>
#include <stack>
using namespace std;
int main()
{
// Initialize a multimap with some entries
multimap<int, string> myMultimap1
= { { 1, "C++" }, { 2, "Java" } };
multimap<int, string> myMultimap2
= { { 1, "Python" }, { 2, "JavaScript" } };
// Create a stack of multimaps
stack<multimap<int, string> > myStack;
myStack.push(myMultimap1);
myStack.push(myMultimap2);
// Print 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:
[ { {1, Python}, {2, JavaScript}, } { {1, C++}, {2, Java}, } ]
Time Complexity: O(N) where n is the number of multimaps.
Auxiliary Space: O(N * M), where M is the average size of the multimaps.
Similar Reads
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 Stack of Multisets in C++? In C++, a stack is a container adapter that operates in a LIFO (Last In First Out) element order and allows insertion and deletion from the end only. A multiset is a container that stores elements in an ordered manner and allows multiple instances of an element. In this article, we will learn how to
2 min read
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 Lists in C++? In C++, a list is a sequence container that allows dynamic insertion and deletion operations, whereas a stack is a data structure that follows last-in, first-out (LIFO). In this article, we will learn how to create a stack of lists in C++. Example: Input: list1 = { 1, 2, 3, 4 }list2 = { 5, 6, 7 }Out
2 min read
How to Create a Multimap of Vectors 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 vectors in C++. For Example, Input:myPa
2 min read
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