包含在头文件include<algorithm.h>中
返回值是bool类型
一般用法为
do{
}while(next_permutation(a,a+n));
不仅可以对数字进行全排列,还可以对字符进行全排列
与之相对应的函数为pre_permutation
示例1:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
int a[n];
for(int i=0; i<n; i++) {
cin>>a[i];
}
sort(a,a+n);
do{
for(int i=0;i<n;i++) {
cout<<a[i]<<" ";
}
cout<<endl;
}while (next_permutation(a,a+n));
return 0;
}
输入:
3
3 1 2
输出:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str;
cin >> str;
sort(str.begin(), str.end());
cout << str << endl;
while (next_permutation(str.begin(), str.end()))
{
cout << str << endl;
}
return 0;
}
输入:
acb
输出:
abc
acb
bac
bca
cab
cba