I thought your code looked a little odd so I tried my own hand at it. Hopefully this is extensible enough that you won't have to use a vector of vectors or a vector of sets:
Code:
// Example program
#include <algorithm>
#include <array>
#include <iostream>
size_t const N = 6;
size_t const K = 3;
template <typename InputIterator>
std::array<int, K> gather(
InputIterator vals_begin,
InputIterator vals_end,
InputIterator bools) {
std::array<int, K> combo{ 0 };
auto it = combo.begin();
size_t i = 0;
for (; vals_begin != vals_end; ++i, ++vals_begin) {
if (bools[i]) {
*it = *vals_begin;
++it;
}
}
return combo;
}
void print_array(std::array<int, K> arr) {
for (auto a : arr)
std::cout << a << " ";
std::cout << std::endl;
}
int main(void) {
std::array<int, N> vals{0, 1, 2, 3, 4, 5};
std::array<int, N> perm;
std::fill(perm.begin(), perm.begin() + K, 1);
std::fill(perm.begin() + K, perm.end(), 0);
int num_combos = 0;
do {
print_array(gather(vals.begin(), vals.end(), perm.begin()));
++num_combos;
} while (std::prev_permutation(perm.begin(), perm.end()));
std::cout << "Total number of combinations : " << num_combos << std::endl;
}
Ouput:
Code:
0 1 2
0 1 3
0 1 4
0 1 5
0 2 3
0 2 4
0 2 5
0 3 4
0 3 5
0 4 5
1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5
Total number of combinations : 20
Here's the link: C++ Shell