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

    优先级队列默认less 大数优先。

    priority_queue<Type, Container, Functional>
    其中Type 为数据类型, Container 为保存数据的容器,Functional 为元素比较方式。
    Container 必须是用数组实现的容器,比如 vector, deque 但不能用 list.
    STL里面默认用的是 vector. 比较方式默认用 operator< , 所以如果你把后面俩个参数缺省的话,
    优先队列就是大顶堆,队头元素最大。

    最常见的两种优先队列的比较方式:  

    std::priority_queue<int,std::vector<int>,std::greater<int> > q; //数据越小,优先级越高
    
    std::priority_queue<int,std::vector<int>,std::less<int> > q;  // 数据越大,优先级越高

    对自己写对结构体比较:

    struct node {
        friend bool operator < (node n1,node n2) {
            return n1.priority < n2.priority;
        }
        int priority;
        int value;
    };
    
    // 自定义结构体的优先队列,我们采用重载 < 运算符
    // 如果a.x < b.x return true 那么就按 x 的优先级从大向小
    // 如果a.x > b.x return true 那么就按 x 的优先级从小往大
  • 相关阅读:
    python之元组
    python之dict
    python之list
    python之str字符串
    python之for循环
    Python的基本语法2
    Python的基本语法1
    初识python
    JS获取当天是周几
    EXCLE导入数据库
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/11980410.html
Copyright © 2011-2022 走看看