首页 > 其他 > 详细

STL之priority_queue(优先队列)

时间:2015-02-09 20:14:25      阅读:311      评论:0      收藏:0      [点我收藏+]

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);
}

 

STL之priority_queue(优先队列)

原文:http://www.cnblogs.com/gaoxing/p/4282259.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!