next_permutation 用于找到下一个全排列
#include<bits/stdc++.h>
using namespace std;
int main( ) {
vector<int> q{1,2,3,4};
for ( int i = 0; i < 24; i++ ) {
for ( int j = 0; j < 4; j++ ) cout << q[j] << " ";
// 寻找下一个全排列
next_permutation(q.begin(), q.end());
cout << endl;
}
return 0;
}
结果:
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1