How to create a Set of Sets in C++


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,

  1. First, we include the necessary header file <unordered_set>.
  2. Next, we create three individual sets of integers named set1, set2, and set3 using std::unordered_set. We insert the elements 1, 2, 3 into set1, 4, 5, 6 into set2, and 7, 8, 9 into set3.
  3. Then, we create a set of sets named setOfSets using std::unordered_set, and we insert set1, set2, and set3 into setOfSets. This nests the three sets within a single set.
  4. 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,

  1. First, we include the necessary header file <unordered_set>.
  2. Next, we create three individual sets of strings named setA, setB, and setC using std::unordered_set. We insert the strings "apple", "banana", "cherry" into setA, "dog", "elephant", "fox" into setB, and "grape", "honeydew", "kiwi" into setC.
  3. Then, we create a set of sets named setOfStringSets using std::unordered_set, and we insert setA, setB, and setC into setOfStringSets. This nests the three sets within a single set.
  4. 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

  1. How to create an Empty Set in C++ ?
  2. How to Initialize a Set in C++ ?
  3. How to Get Length of a Set in C++ ?
  4. How to create a Set of size N in C++ ?
  5. How to create a Set of Numbers from 1 to N in C++ ?
  6. How to create a Set of integers in C++ ?
  7. How to create a Set of Strings in C++ ?
  8. How to Access Items in a Set in C++ ?
  9. How to get a Random Item in a Set in C++ ?
  10. How to Iterate Over a Set in C++ ?
  11. How to check if a Set is Empty in C++ ?
  12. How to check if a Set is Not Empty in C++ ?
  13. How to get Subset from a Set in C++ ?
  14. How to check if a Specific Item is present in the Set in C++ ?
  15. How to check if a Set contains all the items of Another Set in C++ ?
  16. How to Sort Items of a Set in C++ ?
  17. How to Copy a Set in C++ ?
  18. How to add an Item to a Set in C++ ?
  19. How to find Union of Two Sets in C++ ?
  20. How to find Intersection of Two Sets in C++ ?
  21. How to check if Two Sets are Equal in C++ ?
  22. How to Convert a Set of Integers to a Set of Strings in C++ ?
  23. How to Convert a Set of Strings to a Set of Integers in C++ ?
  24. How to Convert a Set of Floats to a Set of Strings in C++ ?
  25. How to Filter Items of a Set based on a Condition in C++ ?
  26. How to Remove Specific Item from a Set in C++ ?
  27. How to Remove Items from Set based on a Condition in C++ ?
  28. How to create a Set of Sets in C++ ?