extract COMBINATIONS to vector

hello.
int ar[9] = {1,2,3,4,5,6,7,8,9};
i have to take 5 numbers(any order) each time
so total combination = 9! /( 5! *(9-5)! ) =126

how to put them in a vector?
vector<int> vect[i]; //push back each series e.g.{1,2,3,4,5} in vect[0] to vect[n]
n= 125 in this case.

vector <vector<int> > vectAll; // push back vect[0] to vect[n]
A simple algorithm is:
1
2
3
4
5
6
for (int i = 0; i < 2n; i++){
    if (number_of_on_bits(i) != k)
        continue;
    for (int j = 0; j < k; j++)
        next_combination[j] = ar[index_of_jth_on_bit(i, j)];
}
n is 9 in your example.
k is 5 in your example.
index_of_jth_on_bit() returns the position of the jth bit that's in the on state. For example,
index_of_jth_on_bit(1100101b, 0) = 0
index_of_jth_on_bit(1100101b, 1) = 2
index_of_jth_on_bit(1100101b, 2) = 5
index_of_jth_on_bit(1100101b, 3) = 6
index_of_jth_on_bit(1100101b, 4 and above) = undefined (only 4 bits are on)
Hi,

Just wondering if you are aware of this STL algorithm?

http://www.cplusplus.com/reference/algorithm/next_permutation/


:+)
I'll use "NcM" to mean "N choose M". Given a set of N elements, NcM is
the first element, appended to (N-1)c(M-1) from the 2nd-Nth elements
the second element appended to (N-1)c(M-1) from the 3rd-Nth elements
the 3rd element appended to (N-1)c(M-1) from the 4th-Nth elements
etc.

Topic archived. No new replies allowed.