Given some arrays, I'd like to loop over every possible permutation of these arrays:
Here's a minimal example:
#include <array>
#include <iostream>
//#include <random>
#include <algorithm>
using namespace std;
int main() {
// Change the first number of the arrays to get a different number of permutations
// (this is not wanted)
array<int, 4> A = { 1,0,0,0 };
array<int, 4> B = { -9,0,0,0 };
array<int, 4> C = { 3,0,0,0 };
array<int, 4> temp[3] = { A, B, C };
int i = 1;
do {
cout << "This is the " << i++ << "-th permutation." << endl;
} while (next_permutation(temp, temp + 3));
// (it should yield 3! = 6 permutations but only yields 4)
return 0;
}
However the number of looped permutations seems to be dependent on the starting value of the array (which is not a feature I wish to use in my project).
This is because next_permutation orders it's elements in lexicographical order.
How can I use this function to get all permutations of the given Arrays? Or do I have to use a different method entirely?
I also know about this answer, but I'd like to avoid sorting my arrays beforehand, since I'm planning to work with a large number of arrays.
Thanks!
tempbefore you start generating permutations.next_permutationdoesn't (can't) store the "original" somehow the first time you call it. If you want an arbitrary starting point, save a copy yourself and keep going until you have reached the initial state.next_permutationon indices, not the actual values. The indices would start out as 0,1,2,3, i.e. sorted already. This is similar to how you would sort items without actually sorting the items (you sort the indexes).