Open In App

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

Last Updated : 19 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, sets are STL containers that store unique elements of the same type in a sorted manner. Sets of sets, also known as nested sets, are collections in which each element of the outer set contains another set as its element. In this article, we will learn how to create a set of sets in C++.

Set of Sets in C++

You can create a set of sets by defining the type of the outer set as another set with a given data type. It will create a set where each element of this set will be a set in itself.

Syntax to Create a Set of Sets

set< set<type> > mySetOfSet;

C++ Program to Create a Set of Set

C++




// C++ program to create a set of Set
#include <iostream>
#include <set>
using namespace std;
  
// Driver Code
int main()
{
    // Creating a set of sets
    set<set<int> > setOfSets;
  
    // creating sets to insert
    set<int> set1 = { 1, 2, 3 };
    set<int> set2 = { 2, 3, 4 };
    set<int> set3 = { 3, 4, 5 };
  
    // Adding sets to the set of sets
    setOfSets.insert(set1);
    setOfSets.insert(set2);
    setOfSets.insert(set3);
  
    // Displaying the sets in the set of sets
    for (const auto& innerSet : setOfSets) {
        for (const auto& element : innerSet) {
            cout << element << " ";
        }
        cout << endl;
    }
  
    return 0;
}


Output

1 2 3 
2 3 4 
3 4 5 

Time Complexity: O(N2)
Auxilairy Space: O(N2)


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads