How to generate POwer set ?
Hello ALL ,
i'm new to C++ and i want to know how to Generate power set using C++ ?
thanks in advance
I think you have to iterate through all the combinations and insert them into a new set manually.
Here is an example: (Note that the code use C++11 so you might want to think of it as pseudo code)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <set>
#include <iostream>
#include <utility>
int main()
{
std::set<int> set{1, 2, 8, 10};
std::set<std::pair<int, int>> power_set;
for (int e1 : set)
{
for (int e2 : set)
{
power_set.insert({e1, e2});
}
}
std::cout << "Power set:";
for (std::pair<int, int> p : power_set)
{
std::cout << " (" << p.first << "," << p.second <<")";
}
std::cout << std::endl;
}
|
Topic archived. No new replies allowed.