prev_permutation与next_permutation

int main(){
int a[] = {3,1,2};
do{
     cout << a[0] << " " << a[1] << " " << a[2] << endl;
}
while (next_permutation(a,a+3));
return 0;
}

输出:312/321         因为原数列不是从最小字典排列开始。

所以要想得到所有全排列

int a[] = {3,1,2};   change to   int a[] = {1,2,3};

另外,库中另一函数prev_permutation与next_permutation相反,由原排列得到字典序中上一次最近排列。

所以

int main(){
int a[] = {3,2,1};
do{
     cout << a[0] << " " << a[1] << " " << a[2] << endl;
}
while (prev_permutation(a,a+3));
return 0;
}

才能得到123的所有排列。



原文地址:https://www.cnblogs.com/qiufeihai/p/2405057.html