How to Create a Stack of Unordered_Multiset in C++?
Last Updated :
18 Mar, 2024
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” }
Output:
Stack of Unordered_Multiset: [ { “orange”, “mango”, “orange” },
{ “apple”, “banana”, “apple” } ]
Stack of Unordered_Multiset in C++
To create a stack of std::unordered_multiset in C++, first declare a std::stack with std::unordered_multiset as the template argument, then use the std::stack::push() function to insert the unordered_multiset in the stack.
Syntax to Create a Stack of Unordered_Multiset in C++
stack<unordered_multiset<datatype>> stack_name;
Here,
- datatype denotes the type of data stored in the unordered_multiset.
- stack_name is the name of the stack of unordered_multiset.
C++ Program to Create a Stack of Unordered_Multiset
The below program demonstrates how we can create and use a stack of unordered_multiset in C++ STL.
C++
// C++ Program to illustrate how to create a stack of
// unordered_multiset
#include <iostream>
#include <stack>
#include <unordered_set>
using namespace std;
int main()
{
// Defining multiple unordered_multisets
unordered_multiset<string> s1
= { "apple", "banana", "apple" };
unordered_multiset<string> s2
= { "orange", "mango", "orange" };
// Create a stack of unordered_multisets
stack<unordered_multiset<string> > stackOfSets;
// Pushing unordered_multisets into the stack
stackOfSets.push(s1);
stackOfSets.push(s2);
// Printing elements from the stack of
// unordered_multisets
cout << "Elements in the Stack of Unordered_Multiset:"
<< endl;
while (!stackOfSets.empty()) {
unordered_multiset<string> currSet
= stackOfSets.top();
stackOfSets.pop();
for (auto& it : currSet) {
cout << it << " ";
}
cout << endl;
}
return 0;
}
OutputElements in the Stack of Unordered_Multiset:
orange orange mango
apple apple banana
Time Complexity: O(N), here N is the total number of elements in each unordered_multiset.
Auxiliary Space: O(M * N), here M is the number of unordered_multiset.
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_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 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
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 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