1,next_permutation(a,a+n);(找下一个)
a代表数组头地址,b代表数组尾地址,如果下一个排列存在则返回真,否者返回假
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int num = 1,a[6]= {1,2,3,4,5};
if(next_permutation(a,a+5)){
for(int i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl;
}else cout<<-1<<endl;
if(next_permutation(a,a+5)){
for(int i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl;
}else cout<<-1<<endl;
return 0;
}
2,prev_permutation(a,a+n);(找上一个)
a代表数组头地址,b代表数组尾地址,如果上一个排列存在则返回真,否者返回假
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int num = 1,a[6]= {1,2,4,3,5};
if(prev_permutation(a,a+5)){
for(int i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl;
}else cout<<-1<<endl;
if(prev_permutation(a,a+5)){
for(int i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl;
}else cout<<-1<<endl;
return 0;
}
3,string同样适用
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string s={'a','b','c','d','e'};
if(next_permutation(s.begin(),s.end()))
cout<<s<<endl;
else
cout<<-1<<endl;
return 0;
}