1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 【C++】next_permutation / prev_permutation函数

【C++】next_permutation / prev_permutation函数

时间:2021-07-14 20:45:00

相关推荐

【C++】next_permutation / prev_permutation函数

关于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进行排序。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。