zoukankan      html  css  js  c++  java
  • priority_queue的优先级变化(结构体的写法)

    priority_queue的优先级变化(结构体的写法)

    在头文件中加上#include <queue> 即可使用stl中的库函数priority_queue,优先队列默认的是从大到小的优先级,但是我们在实际使用的时候,往往需要改变优先级(比如从小到大的排列),这时候就需要改变优先级。

    
    //定义结构,使用运算符重载,自定义优先级1  
    struct cmp1{  
        bool operator ()(int &a,int &b){  
            return a>b;//最小值优先  
        }  
    };  
    struct cmp2{  
        bool operator ()(int &a,int &b){  
            return a<b;//最大值优先  
        }  
    };  
    //定义结构,使用运算符重载,自定义优先级2  
    struct number1{  
        int x;  
        bool operator < (const number1 &a) const {  
            return x>a.x;//最小值优先  
        }  
    };  
    struct number2{  
        int x;  
        bool operator < (const number2 &a) const {  
            return x<a.x;//最大值优先  
        }  
    };  
    
    int main()  
    {   priority_queue<int>que;//采用默认优先级构造队列  
      
        priority_queue<int,vector<int>,cmp1>que1;//最小值优先  
        priority_queue<int,vector<int>,cmp2>que2;//最大值优先  
      
        priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”会被认为错误,  greater为函数从大到小排序
                                                          //这是右移运算符,所以这里用空格号隔开  
        priority_queue<int,vector<int>,less<int> >que4;////最大值优先 less则相反
    
    
  • 相关阅读:
    P3822 [NOI2017]整数
    P4630 [APIO2018] Duathlon 铁人两项
    P3230 [HNOI2013]比赛
    P2570 [ZJOI2010]贪吃的老鼠
    P4576 [CQOI2013]棋盘游戏
    P3256 [JLOI2013]赛车
    P3297 [SDOI2013]逃考
    CF487E Tourists
    设置一个双色球脚本(2)并带颜色输出
    设置一个双色球脚本
  • 原文地址:https://www.cnblogs.com/fzuljz/p/6171963.html
Copyright © 2011-2022 走看看