码迷,mamicode.com
首页 > 其他好文 > 详细

next_permutation函数

时间:2014-09-01 20:52:23      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   for   文件   div   问题   

头文件:

  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的实现应该做的还可以的。

next_permutation函数

标签:style   blog   color   os   io   for   文件   div   问题   

原文地址:http://www.cnblogs.com/dandi/p/3949889.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!