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

转载于:https://www.cnblogs.com/dandi/p/3949889.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值