How to Create Deque of Multiset in C++?
Last Updated :
26 Mar, 2024
In C++, deques are containers that allow efficient insertion and deletion operations from both ends. Multisets are associative containers similar to sets but unlike sets, multisets allow duplicate insertions as well. In this article, we will learn how to create a deque of multiset in C++.
Example
Input:
mySet1 = { 1, 2, 4, 6, 7 }
mySet2 = { 1, 3, 4 , 4 }
Output:
Stack of Unordered_Multiset: [ { 1, 2, 4, 6, 7 },
{ 1, 3, 4, 4 } ]
Create a Deque of Multisets in C++
To create a std::deque of std::multisets in C++, we have to pass the type of the std::deque as std::multiset as the template parameter during the deque declaration. This will create a deque object where each element is a multiset.
Syntax to Create Deque of Multiset in C++
deque<multiset<dataType>> dq_Name;
C++ Program to Create a Deque of Multisets
The following program illustrates how to create a deque of multisets in C++:
C++
// C++ program to illustrate how to create a deque of
// multiset
#include <deque>
#include <iostream>
#include <set>
using namespace std;
int main()
{
// Initialize a deque of multisets
deque<multiset<int> > dq;
// Create multiset with few elemnts
multiset<int> ms1 = { 10, 20, 20, 30 };
multiset<int> ms2 = { 1, 2, 3, 4 };
// Insert the multisets into the deque
dq.push_back(ms1);
dq.push_back(ms2);
// Print the elements of the deque
cout << "Deque Elements: " << endl;
int i = 1;
for (auto it = dq.begin(); it != dq.end(); ++it) {
cout << "MultiSet" << i << ": { ";
for (auto x : *it) {
cout << x << " ";
}
i++;
cout << "}" << endl;
}
return 0;
}
OutputDeque Elements:
MultiSet1: { 10 20 20 30 }
MultiSet2: { 1 2 3 4 }
Time Complexity: O(N*M) where n is the number of multisets.
Auxiliary Space: O(N*M), where M is the average number of elements in the multiset.
Share your thoughts in the comments
Please Login to comment...