In C++, you can create a list of sets using the list container from the STL for the outer list and the set container from the STL for the inner sets.
In this example,
and
.set1
and set2
using the std::set
container.list_of_sets
using the std::list
container and initialize it with the inner sets.#include <iostream>
#include <list>
#include <set>
int main() {
std::set<int> set1 = {1, 2, 3};
std::set<char> set2 = {'a', 'b', 'c'};
std::list<std::set<int>> list_of_sets = {set1, set2};
for (const auto& s : list_of_sets) {
for (const auto& elem : s) {
std::cout << elem << " ";
}
std::cout << std::endl;
}
return 0;
}
1 2 3 a b c
In this example,
and
.empty_set1
and empty_set2
.list_of_empty_sets
using the std::list
container and initialize it with the empty inner sets.#include <iostream>
#include <list>
#include <set>
int main() {
std::set<int> empty_set1;
std::set<char> empty_set2;
std::list<std::set<int>> list_of_empty_sets = {empty_set1, empty_set2};
for (const auto& s : list_of_empty_sets) {
for (const auto& elem : s) {
std::cout << elem << " ";
}
std::cout << std::endl;
}
return 0;
}
In this tutorial, we learned How to create a List of Sets in C++ language with well detailed examples.