C++ STL, next_permutation用法。

next_permutation 将按字母表顺序生成给定序列的下一个较大的序列,直到整个序列为

 1 #include"iostream"
 2 #include"algorithm"
 3 using namespace std;
 4 int main(){
 5     int n,p[10];
 6     cin>>n;
 7     for(int i=0;i<n;i++){
 8         cin>>p[i];
 9     }
10     sort(p,p+n);
11     do{
12         for(int i=0;i<n;i++)
13             cout<<p[i]<<" ";
14         cout<<endl;
15     }while(next_permutation(p,p+n));
16     return 0;
17 }

减序为止。

prev_permutation函数与之相反,是生成给定序列的上一个较小的序列。

二者原理相同,仅遍例顺序相反.

string的nextl;

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 using namespace std;
 5  
 6 int main() {
 7     string s1;
 8     while (cin >>s1&&s1!="#") {
 9         if (next_permutation(s1.begin(),s1.end()))
10             cout <<s1<< endl;
11         else
12             cout <<"No Successor"<<endl;
13     }
14     return 0;
15 }
原文地址:https://www.cnblogs.com/hutonm/p/5365517.html