先来用代码来解释一下next_permutation()函数得用法
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int a[3] = {2, 1, 3};
sort(a, a + 3);
//注意,在使用next_permutation()函数之前,要对数组先按升序排一下序,不然,只能找到这个顺序后的排序,之前的排序不会找到
do{
for (int i = 0; i < 3; i ++ ) cout << a[i] << ' ';
cout << endl;
}
while(next_permutation(a, a + 3));//参数3试是指的数列a要进行排列的长度
/*
next_permutation()函数每一次都会返回一个bool类型的值,
如果现在的排列a下面还有排列(比他字典序大的排列),就会返回true,使得while循环继续
如果现在的排列a接下来没有排列了,就会返回false,使得while终止循环
*/
return 0;
}
/*
输出结果为:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
*/
/*
如果将sort(a, + 3);这一行删掉的话
结果为
2 1 3
2 3 1
3 1 2
3 2 1
*/
/*
如果将while循环里面的(a, a + 3)中的3改为1
就会输出
1 2 3
如果将3改为2
就会输出
1 2 3
2 1 3
这里的3代表了函数next_permutation()将要对数列a进行多长的排列
3改为1,只对序列长度为1的序列进行排序,那么就是只对1进行排序
3改为2,只对序列长度为2的序列进行排序,那么就是只对1和2进行排序
*/
next_permutation()函数是一个求一个排序的下一个排序的函数,他的头文件为#include ,并且支持自定义cmp排序
更加详细的了解,可以查看这里
1、next_permutation(a,a+n)
2、【用法总结】C++ STL中 next_permutation函数的用法
3、next_permutation(全排列算法)
4、next_permutation函数