本文最后更新于 482 天前,其中的信息可能已经有所发展或是发生改变。

具体内容
1.函数原型:bool next_permutation(iterator start, iterator end);
2.返回值:布尔型
3.函数本体:
1)next_permutation(开始,结束),输出所有比当前排列大的排列,顺序是从小到大。
2)prev_permutation(开始,结束),输出所有比当前排列小的排列,顺序是从大到小。
4.这两个函数的排列区间都是左闭右开,如 next_permutation(a,a+10) ,被全排列的区间为[0,10),即数组中下标为 0 1 2 3 4 5 6 7 8 9 的元素。
示例代码
#include<iostream>
using namespace std;
#include<algorithm> //为了使用 next_permutation() 函数
int main()
{
int a[4]={0,1,2,3};
while(next_permutation(a+1,a+4)) //全排列函数,对数组中下标为 1 ~ 3 的元素进行全排列
{
for(int i=1;i<=3;i++)
cout<<a[i]<<" ";
cout<<endl;
}
return 0;
}

具体操作可以看一下下面这个题
描述
将 1∼n 这 n 个整数排成一行后随机打乱顺序,输出所有可能的次序。
输入描述
一个整数 n。
输出描述
按照从小到大的顺序输出所有方案,每行 1 个。
- 同一行相邻两个数用一个空格隔开。
- 对于两个不同的行,对应下标的数一一比较,字典序较小的排在前面。
用例输入
3
用例输出
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
1 ≤ n ≤ 9- 为方便测评,请按照字典序从小到大依次输出
ac代码
#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
int main()
{
int n;
int a[100010];
cin>>n;
for(int i=1;i<=n;i++)
{
a[i]=i;
}
for(int i=1;i<=n;i++)//因为全排列函数不输出原来的代码所以我们自己先输出一遍原来的代码
{
cout<<a[i]<<" ";
}
cout<<endl;
while(next_permutation(a+1,a+n+1))
{
for(int i=1;i<=n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
return 0;
}

prev_permutation就不展示了 就是上面的这个图倒着输出