How to Create a Set of Sets in C++?
Last Updated :
19 Feb, 2024
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++
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<set< int > > setOfSets;
set< int > set1 = { 1, 2, 3 };
set< int > set2 = { 2, 3, 4 };
set< int > set3 = { 3, 4, 5 };
setOfSets.insert(set1);
setOfSets.insert(set2);
setOfSets.insert(set3);
for ( const auto & innerSet : setOfSets) {
for ( const auto & element : innerSet) {
cout << element << " " ;
}
cout << endl;
}
return 0;
}
|
Time Complexity: O(N2)
Auxilairy Space: O(N2)
Share your thoughts in the comments
Please Login to comment...