-
For the record, we can also use standard library now for these permutations, avoiding extra code : http://rosettacode.org/wiki/Combinations#C.2B.2B
In the Rosetta code example, we do the permutation on a bitfield that can be used as a mask on the object array later to get the permutations we want
#include <algorithm> #include <iostream> #include <string> void comb(int N, int K) { std::string bitmask(K, 1); // K leading 1's bitmask.resize(N, 0); // N-K trailing 0's // print integers and permute bitmask do { for (int i = 0; i < N; ++i) // [0..N-1] integers { if (bitmask[i]) std::cout << " " << i; } std::cout << std::endl; } while (std::prev_permutation(bitmask.begin(), bitmask.end())); } int main() { comb(5, 3); }
Please register or sign in to comment