STL中的全排列next_permutation

next_permutation的函数声明:#include 

 

bool next_permutation( iterator start, iterator end);


next_permutation函数的返回值是布尔类型,在STL中还有perv_permutation()函数

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include   
  2. #include   
  3. #include   
  4.   
  5. using namespace std;  
  6.   
  7. int main()  
  8. {  
  9.     string str;  
  10.     cin >> str;  
  11.     sort(str.begin(),str.end());  
  12.     while (next_permutation(str.begin(), str.end()))  
  13.         cout << str << endl;  
  14.     return 0;  
  15. }  


next_permutation()函数功能是输出所有比当前排列大的排列,顺序是从小到大。

而prev_permutation()函数功能是输出所有比当前排列小的排列,顺序是从大到小。

//@auther Yang Zongjun
#include 
#include 
#include 
#include 
#include 
#include using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
const int MAXN = 1100000;
const int INF = 2100000000;
int n, per[MAXN];void permutation(int n)
{for(int i = 0; i < n; i++){per[i] = i;}do{for(int i = 0; i< n; i++)cout << per[i] << " ";cout << endl;}while(next_permutation(per, per + n));
}int main()
{//freopen("C:/Users/Administrator/Desktop/input.txt", "r", stdin);cin >> n;permutation(n);return 0;
}



本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部