std::next_permutation may help:
#include <algorithm> #include <iostream> int main() { int v[] = {1, 2, 3, 4, 5, 6}; do { std::cout << "{" << v[0] << "," << v[1] << "," << v[2] << "}, " << "{" << v[3] << "," << v[4] << "," << v[5] << "}" << std::endl; } while (std::next_permutation(std::begin(v), std::end(v))); return 0; }
which output (6! solutions)
{1,2,3}, {4,5,6} {1,2,3}, {4,6,5} ... {6,5,4}, {3,1,2} {6,5,4}, {3,2,1}
Or if order inside group doesn't matter, you may try the following:
#include <algorithm> #include <iostream> int getNextIndex(const int (&v)[6], int start, int value) { return std::find(std::begin(v) + start, std::end(v), value) - std::begin(v); } void print(const int (&v)[6]) { // Filter if you want that // `{1, 2, 3}, {4, 5, 6}` is equivalent to `{4, 5, 6}, {1, 2, 3}` if (getNextIndex(v, 0, 1) > getNextIndex(v, 0, 2)) return; //if (getNextIndex(v, 0, 2) > getNextIndex(v, 0, 3)) return; // And so on if you have more groups const char* sep[] = {", ", ", ", "}"}; for (int i = 1; i != 3; ++i) { int index = -1; std::cout << "{"; for (int j = 0; j != 3; ++j) { index = getNextIndex(v, index + 1, i); std::cout << 1 + index << sep[j]; } std::cout << ", "; } std::cout << std::endl; } int main() { int v[] = {1, 1, 1, 2, 2, 2}; do { print(v); } while (std::next_permutation(std::begin(v), std::end(v))); return 0; }
which outputs (10 solutions):
{1, 2, 3}, {4, 5, 6}, {1, 2, 4}, {3, 5, 6}, {1, 2, 5}, {3, 4, 6}, {1, 2, 6}, {3, 4, 5}, {1, 3, 4}, {2, 5, 6}, {1, 3, 5}, {2, 4, 6}, {1, 3, 6}, {2, 4, 5}, {1, 4, 5}, {2, 3, 6}, {1, 4, 6}, {2, 3, 5}, {1, 5, 6}, {2, 3, 4},