zoukankan      html  css  js  c++  java
  • 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的实现应该做的还可以的。

  • 相关阅读:
    Day 22 初识面向对象
    Day 21 内存处理与正则
    Day 20 常用模块(三)
    Day 18 常用模块(二)
    url解析
    jQuery---扩展事件
    jQuery---文档操作
    jQuery---属性操作
    jQuery---基本语法
    CSS---常用属性总结
  • 原文地址:https://www.cnblogs.com/dandi/p/3949889.html
Copyright © 2011-2022 走看看