How to create a Set of Sets in C++ - Step by Step Examples
How to create a Set of Sets in C++ ?
Answer
To create a Set of Sets in C++, you can use nested std::unordered_set containers. You create individual sets using std::unordered_set and then add them to another std::unordered_set.
✐ Examples
1 Creating a Set of Sets of Integers
In this example,
- First, we include the necessary header file
<unordered_set>
. - Next, we create three individual sets of integers named
set1
,set2
, andset3
usingstd::unordered_set
. We insert the elements1, 2, 3
intoset1
,4, 5, 6
intoset2
, and7, 8, 9
intoset3
. - Then, we create a set of sets named
setOfSets
usingstd::unordered_set
, and we insertset1
,set2
, andset3
intosetOfSets
. This nests the three sets within a single set. - Finally, we iterate over each set within
setOfSets
using a range-based for loop. Within the loop, we print each individual set to the standard output.
C++ Program
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> set1 = {1, 2, 3};
std::unordered_set<int> set2 = {4, 5, 6};
std::unordered_set<int> set3 = {7, 8, 9};
std::unordered_set<std::unordered_set<int>> setOfSets = {set1, set2, set3};
for (const auto& set : setOfSets) {
for (const auto& element : set) {
std::cout << element << " ";
}
std::cout << std::endl;
}
return 0;
}
Output
Output: 1 2 3 4 5 6 7 8 9
2 Creating a Set of Sets of Strings
In this example,
- First, we include the necessary header file
<unordered_set>
. - Next, we create three individual sets of strings named
setA
,setB
, andsetC
usingstd::unordered_set
. We insert the strings"apple", "banana", "cherry"
intosetA
,"dog", "elephant", "fox"
intosetB
, and"grape", "honeydew", "kiwi"
intosetC
. - Then, we create a set of sets named
setOfStringSets
usingstd::unordered_set
, and we insertsetA
,setB
, andsetC
intosetOfStringSets
. This nests the three sets within a single set. - Finally, we iterate over each set within
setOfStringSets
using a range-based for loop. Within the loop, we print each individual set to the standard output.
C++ Program
#include <iostream>
#include <unordered_set>
#include <string>
int main() {
std::unordered_set<std::string> setA = {"apple", "banana", "cherry"};
std::unordered_set<std::string> setB = {"dog", "elephant", "fox"};
std::unordered_set<std::string> setC = {"grape", "honeydew", "kiwi"};
std::unordered_set<std::unordered_set<std::string>> setOfStringSets = {setA, setB, setC};
for (const auto& set : setOfStringSets) {
for (const auto& element : set) {
std::cout << element << " ";
}
std::cout << std::endl;
}
return 0;
}
Output
Output: apple cherry banana fox dog elephant honeydew grape kiwi
Summary
In this tutorial, we learned How to create a Set of Sets in C++ language with well detailed examples.
More C++ Sets Tutorials
- How to create an Empty Set in C++ ?
- How to Initialize a Set in C++ ?
- How to Get Length of a Set in C++ ?
- How to create a Set of size N in C++ ?
- How to create a Set of Numbers from 1 to N in C++ ?
- How to create a Set of integers in C++ ?
- How to create a Set of Strings in C++ ?
- How to Access Items in a Set in C++ ?
- How to get a Random Item in a Set in C++ ?
- How to Iterate Over a Set in C++ ?
- How to check if a Set is Empty in C++ ?
- How to check if a Set is Not Empty in C++ ?
- How to get Subset from a Set in C++ ?
- How to check if a Specific Item is present in the Set in C++ ?
- How to check if a Set contains all the items of Another Set in C++ ?
- How to Sort Items of a Set in C++ ?
- How to Copy a Set in C++ ?
- How to add an Item to a Set in C++ ?
- How to find Union of Two Sets in C++ ?
- How to find Intersection of Two Sets in C++ ?
- How to check if Two Sets are Equal in C++ ?
- How to Convert a Set of Integers to a Set of Strings in C++ ?
- How to Convert a Set of Strings to a Set of Integers in C++ ?
- How to Convert a Set of Floats to a Set of Strings in C++ ?
- How to Filter Items of a Set based on a Condition in C++ ?
- How to Remove Specific Item from a Set in C++ ?
- How to Remove Items from Set based on a Condition in C++ ?
- How to create a Set of Sets in C++ ?