关于next_permutation函数
next_permutation和prev_permutation函数都是C++STL中的全排列函数。
函数原型:
#include < algorithm >
bool next_permutation(iterator start,iterator end)
bool prev_permutation(iterator start,iterator end)
next_permutation()函数是输出所有比当前排列大的排列,顺序是从小到大。
prev_permutation()函数是输出所有比当前排列小的排列,顺序是从大到小。
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int num[4]={3,2,1};
string s="123";
do{
cout<<s<<endl;
}while(next_permutation(s.begin(),s.end()));
puts("");
do{
cout<<num[0]<<' '<<num[1]<<' '<<num[2]<<endl;
}while(prev_permutation(num,num+3));
return 0;
}
Output:
123
132
213
231
312
321
3 2 1
3 1 2
2 3 1
2 1 3
1 3 2
1 2 3
next_permutation(node,node+n,cmp)还可以对结构体num按照自定义的排序方式cmp进行排序。