Suppose I have a vector of n values, I want to get the different combinations of its values, for example: if I have vect = [a, b, c] the different combinations that I want are: [a, b, c], [a,b], [a,c], [b,c], [a], [b], [c]
Note that for example [a,b] is the same as [b,a] so I don't need to keep both of them.
Give you a pseudo code, please convert it to real code.
Imagine you have a function which can already do this for you. Let's call it
combinations
.If you were going to implement your own version of this,
my_combinations
, you could do it by looking at the first element in your vector, callingcombinations
on the rest of the vector, and then combining your element with each of the combinations.Once you'd implemented this, you could delegate to your own version of
combinations
instead of using the pre-existing one.Count from
0
to2^vector.size() - 1
. If bit i of your loop variable is 1, includevector[i]
in your combination.Edit: if you want to exclude the empty set, start counting at 1.