How to Create Deque of Unordered_Set in C++?
Last Updated :
27 Mar, 2024
In C++, a deque (double-ended queue) is a container that allows insertion and removal of elements from both ends whereas an unordered set is a container that stores unique elements in no particular order. In this article, we will learn how to create a deque of unordered sets in C++ STL.
Example:
Input:
myUSet1 = {1, 2, 3}
myUSet2 = {4, 5, 6}
Output:
Deque Elements:
uSet1: 3 1 2
uSet2: 6 4 5
Creating Deque of Unordered_Set in C++
To create a std::deque of std::unordered_set, we can use the std::deque template class that takes the type of the elements as a template parameter. We can pass the type as an unordered_set of a given type.
Syntax to Declare Deque of Unordered Set
deque<unordered_set<Type>> myDeque;
C++ Program to Create Deque of Unordered_Set
The below program demonstrates how we can create a deque of unordered_sets in C++.
C++
// C++ program to illustrate how to create a deque of
// unordered sets
#include <deque>
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
// Declaring a deque of unordered sets
deque<unordered_set<int> > myDeque;
// Creating some unordered sets
unordered_set<int> set1 = { 1, 2, 3 };
unordered_set<int> set2 = { 4, 5, 6 };
// Pushing unordered sets into the deque
myDeque.push_back(set1);
myDeque.push_back(set2);
// Checking if the deque is empty
cout << "Deque Elements: " << endl;
int count = 1;
for (auto& ele : myDeque) {
cout << "uSet" << count << ": ";
for (auto i : ele) {
cout << i << " ";
}
count++;
cout << endl;
}
return 0;
}
OutputDeque Elements:
uSet1: 3 1 2
uSet2: 6 4 5
Time Complexity: O(N*M), where N is the number of sets and M is the average size of each set.
Auxilliary Space: O(N*M)
Share your thoughts in the comments
Please Login to comment...