next_permutation函数

头文件:

  algorithm

参数: 

  next_permutation(first,last)   

  next_permutation(first,last,cmp)

first,last为两个iterator,分别指向目标的头和尾,cmp是一个bool函数,接受两个目标序列值,返回bool

next_permutation函数每次返回0..1,并且如果可以,把目标序列变成下一个排列。

 

我的样例代码:

 1 #include <iostream>
 2 #include <algorithm>
 3 
 4 using namespace std;
 5 
 6 const int Maxn=1000;
 7 
 8 bool cmp(int a,int b)
 9 {
10     return a<b;
11 }
12 
13 int main()
14 {
15     int dat[Maxn];
16     int N=5;
17 
18     for (int i=1;i<=N;i++)
19     {
20         dat[i]=i;
21     }
22 
23     do
24     {
25         for (int i=1;i<=N;i++)
26         {
27             cout<<dat[i]<<" ";
28         }
29         cout<<endl;
30     }while (next_permutation(dat+1,dat+N+1,cmp));
31 
32 }

 

关于效率问题...回头自己写一个DFS跟他的比较一下吧,感觉STL的实现应该做的还可以的。

posted on 2014-09-01 19:30  杰斯特丹第  阅读(211)  评论(0编辑  收藏  举报

导航