Open In App

How to Create a Deque of Sets in C++?

Last Updated : 27 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, a container called deque is a queue like container but allows for fast insertions and deletions at both ends. In this article, we will learn how to create a deque of sets in C++.

Example:

Input: 
mySet1 = {1, 4, 8, 9, 11} 
mySet2 = {1, 2, 3, 5, 7}

Output: 
myDeque: [ {1, 4, 8, 9, 11}, {1, 2, 3, 5, 7} ]

Creating a Deque of Sets in C++

To create a std::deque of std::set in C++, we can simply pass the type of the deque container to be of type std::set as a template parameter while declaration.

Syntax to Declare Deque of Set

deque<set<type>> myDeque

C++ Program to Create a Deque of Sets

The below example demonstrates how we can create a deque of sets in C++ STL.

C++
// C++ Program to illustrate how to create a deque of set
#include <deque>
#include <iostream>
#include <set>
using namespace std;

int main()
{
    // Declaring a deque of sets
    deque<set<int> > myDeque;

    // Creating some sets
    set<int> set1 = { 1, 2, 3 };
    set<int> set2 = { 4, 5, 6 };
    set<int> set3 = { 7, 8, 9 };

    // Pushing sets into the deque
    myDeque.push_back(set1);
    myDeque.push_back(set2);
    myDeque.push_back(set3);

    // Checking if the deque is empty
    cout << "myDeque: " << endl;
    int i = 1;
    for (auto& ele : myDeque) {
        cout << "Set" << i++ << ": ";
        for (auto i : ele) {
            cout << i << " ";
        }
        cout << endl;
    }

    return 0;
}

Output
myDeque: 
Set1: 1 2 3 
Set2: 4 5 6 
Set3: 7 8 9 

Time complexity: O(N*M), where N is the number of sets.
Auxiliary Space: O(N*M), where M is the average size of the set.




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads