http://www.cppblog.com/Darren/archive/2009/06/09/87224.html

priority_queue 调用 STL里面的 make_heap(), pop_heap(), push_heap() 算法
实现,也算是堆的另外一种形式。

先写一个用 STL 里面堆算法实现的与真正的STL里面的 priority_queue  用法相
似的 priority_queue, 以加深对 priority_queue 的理解


#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

class priority_queue
{
    private:
        vector<int> data;
        
    public:
        void push( int t ){ 
            data.push_back(t); 
            push_heap( data.begin(), data.end()); 
        }
        
        void pop(){
            pop_heap( data.begin(), data.end() );
            data.pop_back();
        }
        
        int top()    { return data.front(); }
        int size()   { return data.size();  }
        bool empty() { return data.empty(); }
};


int main()
{
    priority_queue  test;
    test.push( 3 );
    test.push( 5 );
    test.push( 2 );
    test.push( 4 );
    
    while( !test.empty() ){
        cout << test.top() << endl;
        test.pop(); }
        
    return 0;
}

STL里面的 priority_queue 写法与此相似,只是增加了模板及相关的迭代器什么的。

priority_queue 对于基本类型的使用方法相对简单。
他的模板声明带有三个参数,priority_queue<Type, Container, Functional>
Type 为数据类型, Container 为保存数据的容器,Functional 为元素比较方式。
Container 必须是用数组实现的容器,比如 vector, deque 但不能用 list.
STL里面默认用的是 vector. 比较方式默认用 operator< , 所以如果你把后面俩个
参数缺省的话,优先队列就是大顶堆,队头元素最大。
看例子


#include <iostream>
#include <queue>

using namespace std;

int main(){
    priority_queue<int> q;
    
    for( int i= 0; i< 10; ++i ) q.push( rand() );
    while( !q.empty() ){
        cout << q.top() << endl;
        q.pop();
    }
    
    getchar();
    return 0;
}

如果要用到小顶堆,则一般要把模板的三个参数都带进去。
STL里面定义了一个仿函数 greater<>,对于基本类型可以用这个仿函数声明小顶堆
例子:


#include <iostream>
#include <queue>

using namespace std;

int main(){
    priority_queue<int, vector<int>, greater<int> > q;
    
    for( int i= 0; i< 10; ++i ) q.push( rand() );
    while( !q.empty() ){
        cout << q.top() << endl;
        q.pop();
    }
    
    getchar();
    return 0;
}

对于自定义类型,则必须自己重载 operator< 或者自己写仿函数
先看看例子:


#include <iostream>
#include <queue>

using namespace std;

struct Node{
    int x, y;
    Node( int a= 0, int b= 0 ):
        x(a), y(b) {}
};

bool operator<( Node a, Node b ){
    if( a.x== b.x ) return a.y> b.y;
    return a.x> b.x; 
}

int main(){
    priority_queue<Node> q;
    
    for( int i= 0; i< 10; ++i )
    q.push( Node( rand(), rand() ) );
    
    while( !q.empty() ){
        cout << q.top().x << ' ' << q.top().y << endl;
        q.pop();
    }
    
    getchar();
    return 0;
}

自定义类型重载 operator< 后,声明对象时就可以只带一个模板参数。
但此时不能像基本类型这样声明
priority_queue<Node, vector<Node>, greater<Node> >;
原因是 greater<Node> 没有定义,如果想用这种方法定义
则可以按如下方式

例子:

#include <iostream>
#include <queue> using namespace std; struct Node{
int x, y;
Node( int a= , int b= ):
x(a), y(b) {}
}; struct cmp{
bool operator() ( Node a, Node b ){
if( a.x== b.x ) return a.y> b.y; return a.x> b.x; }
}; int main(){
priority_queue<Node, vector<Node>, cmp> q; for( int i= ; i< ; ++i )
q.push( Node( rand(), rand() ) ); while( !q.empty() ){
cout << q.top().x << ' ' << q.top().y << endl;
q.pop();
} getchar();
return ;
}


priority_queue优先级队列总结的更多相关文章

  1. C++STL模板库适配器之优先级队列

    目录 适配器之优先级队列 一丶优先级队列简介(priority_queue) 二丶优先级队列代码演示 1.优先级队列代码以及使用简介 适配器之优先级队列 一丶优先级队列简介(priority_queu ...

  2. STL之heap与优先级队列Priority Queue详解

    一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的m ...

  3. STL学习系列七:优先级队列priority_queue容器

    1.简介 最大值优先级队列.最小值优先级队列 优先级队列适配器 STL priority_queue 用来开发一些特殊的应用,请对stl的类库,多做扩展性学习 这里给个例子: #include< ...

  4. STL之优先级队列priority_queue

    摘要: priority_queue,自适应容器(即容器适配器):不能由list来组建: 最大值优先级队列(最大值始终在对首,push进去时候) 最小值优先级队列: 优先级队列适配器 STL  pri ...

  5. 优先级队列用法详解(priority_queue)

    由于优先级队列的内部数据结构为 堆,所以这里先介绍堆的一些操作. 堆的一些函数操作在algorithm头文件中 //在[first, last)范围内构造最大堆,first,last 可以是vecto ...

  6. C++ STL 学习笔记__(6)优先级队列priority_queue基本操作

    10.2.7优先级队列priority_queue v  最大值优先级队列.最小值优先级队列 v  优先级队列适配器 STL priority_queue v  用来开发一些特殊的应用,请对stl的类 ...

  7. STL中的优先级队列priority_queue

    priority_queue(queue类似)完全以底部容器为根据,再加上二叉堆(大根堆或者小根堆)的实现原理,所以其实现非常简单,缺省情况下priority_queue以vector作为底部容器.另 ...

  8. C++ - 库函数优先级队列(priority_queue)输出最小值 代码

    库函数优先级队列(priority_queue)输出最小值 代码 本文地址: http://blog.csdn.net/caroline_wendy 库函数优先级队列(priority_queue)的 ...

  9. c++ 优先级队列(priority_queue)

    从网上搜优先级队列用法,都是有些乱七八糟的,有几种用法都没说,直接贴代码.实在郁闷,于是自己在此归纳归纳. 废话不多说,直入主题. 优先级队列的核心是比较函数的实现. 比较函数有两种实现方法: 1.在 ...

随机推荐

  1. Qt连接mysql数据库遇到QMYSQL driver not loaded

    本文件向各位博友分享一下我在Qt开发过程中,连接mysql数据库时遇到的问题,以及解决的方法,希望对遇到同样问题的博友有所帮助. 工程运行环境:vs2015+Qt5.8 在开发过程中,编写数据库连接函 ...

  2. JavaScript 深度遍历对象的两种方式,递归与非递归

    递归遍历: 基本问题: 当前属性值不为对象时,打印键和值 递归过程:当前属性值为对象时,打印键,继续递归 var o = { a: { b: { c: { d: { e: { f: 1, g:{ h: ...

  3. mqtt开源服务器 EMQX ,客户端MQTTX5.0,使用指南

    服务器 EMQX 官网: https://docs.emqx.io/broker/v3/cn/getstarted.html#mqtt-clients 一.安装启动 # 各平台下载https://ww ...

  4. Build ear package

    build 单个service ear TestService -> TestService 修改file Location地址(放在你指定的位置) 点击Build Archive succes ...

  5. @ResponseBody是如何起作用的

    前言 最近参与的项目中,接口中返回的日期格式不对,发现项目中配置了fastjson作为spring的数据转换器,于是使用了fastjson的字段格式化转换注解 发现不起作用.这让我很疑惑,然后在fas ...

  6. 刚下载好的 vscode 不能运行,一片黑 以及终端不能输入 解决办法

    1.鼠标右键vscode快捷方式点击属性,选择兼容性,勾选以兼容模式运行,下拉列表调整为windows vista (service pack 1)即可解决. 2.如果打开终端不能输入命令,首先点击属 ...

  7. Windows平台VC++ 6.0 下的网络编程学习 - 简单的测试winsock.h头文件

    最近学习数据结构和算法学得有点累了(貌似也没那么累...)...找了本网络编程翻了翻当做打一个小基础吧,打算一边继续学习数据结构一边也看看网络编程相关的... 简单的第一次尝试,就大致梳理一下看书+自 ...

  8. how to analyze jmeter results

    https://octoperf.com/blog/2017/10/19/how-to-analyze-jmeter-results/

  9. Day11 - B - Dice (III) LightOJ - 1248

    设dp_i为已经出现了i面,需要的期望次数,dp_n=0 那么dp_i= i/n*dp_i + (n-i)/n*dp_(i+1) + 1 现在已经i面了,i/n的概率再选择一次i面,(n-i)/n的概 ...

  10. a链接内容过长,换行

    上图为溢出情况,此情况均为 英文或数字,但亲测,中文也可正常换行. 添加   word-wrap: break-word;  后,正常换行. 若不希望换行,设为  white-space: nowra ...