• Priority_queue详解


    Priority_queue详解

      1.基本函数

      2.定义

    基本函数: 

      .push(x)

      .pop()

      .top()

      .size()

      .empty()

    定义:

      

      priority_queue<Type, Container, Functional>
      Type 就是数据类型,Container 就是容器类型(Container必须是用数组实现的容器,比如vector,deque等等,但不能用 list。STL里面默认用的是vector),Functional 就是比较的方式,当需要用自定义的数据类型时才需要传入这三个参数,使用基本数据类型时,只需要传入数据类型,默认是大顶堆
    一般是:

      对于基础类型 默认是大顶堆

      priority_queue <int,vector<int>,greater<int> > q;   升序
      priority_queue <int,vector<int>,less<int> > q;    降序

      //greater和less是std实现的两个仿函数

      头文件 include<queue> include<iterator>

      队列 priority_queue<类型> 名称

      越大越优先

      优先级自定义(运算符重载):

      Struct Node{
    
          int  w,v
    
        }
    
        bool operator <(const Node &a,const Node &b){
    
          return a.v>b.v   (按照V从小到大输出)
    
        }

     自定义优先队列

    #include<queue>
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #define FORa(i,s,e) for(int i=s;i<=e;i++)
    #define FORs(i,s,e) for(int i=s;i>=e;i--)
    #define gc pa==pb&&(pb=(pa=buf)+fread(buf,1,10000,stdin),pa=pb)?EOF:*pa++
    #define File(name) freopen(name".in","r",stdin),freopen(name".out","w",stdout);
    
    using namespace std;
    static char buf[10000],*pa=buf,*pb=buf;
    inline int read();
    struct Node{
        int x,y;
        bool operator >(const Node &a) const
        {
            return x>a.x;  //比较大小
        }
        void print() const
        {
            cout<<x<<" "<<y<<endl; //结构体内部函数 
        }
    }a[100];
    int main()
    {
        priority_queue<Node,vector<Node>,greater<Node> > pq_a;  //比较的要与结构体定义函数行的符号一致
        FORa(i,1,10)
        {
            scanf("%d%d",&a[0].x,&a[0].y); 
            pq_a.push(Node(a[0]));
         } 
        while(!pq_a.empty())
       {
               a[0]=pq_a.top();
               a[0].print();
               pq_a.pop();
       }
        return 0;
    }
    
    inline int read()
    {
        register int x(0);register int f(1);register char c(gc);
        while(c<'0'||c>'9') f=c=='-'?-1:1;
        while(c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=gc;
        return x;
    }

     

  • 相关阅读:
    设计模式---策略模式
    maven+eclipse创建web项目
    【Mybatis】多对多实例
    【Mybatis】一对多实例
    【Mybatis】一对一实例
    S5PV210的开发与学习:2.6 UBOOT学习笔记(uboot源码分析3-uboot如何启动内核)
    S5PV210的开发与学习:2.5 UBOOT学习笔记(uboot源码分析2-启动第二阶段)
    S5PV210的开发与学习:2.4 UBOOT学习笔记(uboot源码分析1-启动第一阶段)
    uboot 移植文件差异比较报告
    S5PV210的开发与学习:2.3 UBOOT学习笔记(uboot配置和编译过程详解)
  • 原文地址:https://www.cnblogs.com/SeanOcean/p/10366816.html
Copyright © 2020-2023  润新知