next_permutation()函数 和 prev_permutation() 按字典序求全排列

next_permutation功能:    求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>

  与之完全相反的函数还有prev_permutation

 

这个博客介绍的比较好

 

自己写了一个用法的样例:

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     char s[100];
 9     int len, cnt;
10     while(cin>>s)
11     {
12         len = strlen(s);
13         cnt = 0;
14         sort(s, s+len);
15         do
16         {
17             cout<<s<<endl;
18             cnt++;
19         }while(next_permutation(s, s+len));
20         cout<<cnt<<endl;
21     }
22     return 0;
23 }

 

posted @ 2014-04-11 20:57  水门  阅读(205)  评论(0编辑  收藏  举报