zoukankan      html  css  js  c++  java
  • STL之priority_queue(优先队列)

    priority_queue是一个容器适配器,在这个容器里第一个数据元素是最大的。它的使用场景是什么样:如果12306抢票,为什么黄牛能抢这么多票,感觉12306那边的请求队列是一个优先队列,黄牛的请求携带了一个隐含参数,所以他的请求最先执行。当然这是怀疑。不过也是优先级队列的使用场景。还可以进行排序,把数据压入优先队列中,然后出队列就是由大到小排列了

    注意:stl的priority_queue容器需要一个boolean operator<(const T& ,const T&)函数,注意是函数不是方法。

    #include <iostream>
    #include <queue>
    using namespace std;
    
    typedef struct
    {
        int a;
        string b;
    } Item;
    
    bool operator < ( const Item &left,const Item &right )
    {
        if(left.a<right.a){
        return true;
        }else{
        return false;
        }
    }
    
    int main()
    {
        std::priority_queue< Item >        item_quene ;
        Item t1;
        t1.a=2;
        t1.b="gaoxing";
        item_quene.push(t1);
        Item t2;
        t2.a=3;
        t2.b="nihao";
        item_quene.push(t2);
    }
  • 相关阅读:
    数据结构(2)
    python数据结构(1)
    python 中__getitem__ 和 __iter__ 的区别
    python 中的 %s,%r,__str__,__repr__
    python中的zip
    python反射,单例模式
    类python中高级用法
    python中super与成员属性
    python 类与对象解析
    【其他】BootCDN
  • 原文地址:https://www.cnblogs.com/gaoxing/p/4282259.html
Copyright © 2011-2022 走看看