zoukankan      html  css  js  c++  java
  • 自定义优先队列

    priority_queue<int,vector<int>,greater<int> >que;//最小值优先
    priority_queue<int,vector<int>,less<int> >que;//最大值优先
    //定义结构,使用运算符重载,自定义优先级  
    struct cmp1{  
        bool operator ()(int &a,int &b){  
            return a>b;//最小值优先  
        }  
    };  
    struct cmp2{  
        bool operator ()(int &a,int &b){  
            return a<b;//最大值优先  
        }  
    };  
    priority_queue<int,vector<int>,cmp1>que1;//最小值优先
    priority_queue<int,vector<int>,cmp2>que2;//最大值优先

    #include<queue>
    #include<utility>
    #include<iostream>
    using namespace std;
    struct number{
      int x,y;
      number(int xx,int yy){
        x=xx,y=yy;
      }
      bool operator < (const number &a)const{
      return x>a.x;//最小值优先
      }
    };

    int main()
    {
      priority_queue<number>que;
      que.push(number(3,1));
      que.push(number(1,2));
      que.push(number(2,3));
      while(!que.empty()){
      cout<<que.top().x<<' '<<que.top().y<<endl;
      que.pop();
      }
    }
    /*输出
    1 2
    2 3
    3 1
    */

    
    
      
     
  • 相关阅读:
    CodeForces
    网络流
    poj 2185
    树的分治学习
    数位DP
    URAL 1969. Hong Kong Tram
    hdu 4759 Poker Shuffle
    hdu3712 Detector Placement
    分块思想
    莫比乌斯反演
  • 原文地址:https://www.cnblogs.com/freinds/p/6294144.html
Copyright © 2011-2022 走看看