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= 0, int b= 0 ):
        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= 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;
}
 
 
//以上代码实现的是一个小顶堆

转载:http://blog.chinaunix.net/space.php?uid=533684&do=blog&cuid=2615612

ps:如果重载operator > 可直接使用priority_queue<Node,vector<Node>,greater<Node>>

(转)priority_queue的用法的更多相关文章

  1. STL priority_queue 常见用法详解

    <算法笔记>学习笔记 priority_queue 常见用法详解 //priority_queue又称优先队列,其底层时用堆来实现的. //在优先队列中,队首元素一定是当前队列中优先级最高 ...

  2. priority_queue 优先队列用法

    //采用默认优先关系: //(priority_queue<int>que;) //Queue 0: // 91 83 72 56 47 36 22 14 10 7 3 // //采用结构 ...

  3. 【STL】-priority_queue的用法

    初始化: priority_queue<int>                           maxPQ; priority_queue<int,vector<int& ...

  4. priority_queue的用法

    priority_queue本质是一个堆. 1. 头文件是#include<queue> 2. 关于priority_queue中元素的比较 模板申明带3个参数:priority_queu ...

  5. 【转】priority_queue的用法

    http://www.cnblogs.com/flyoung2008/articles/2136485.html priority_queue调用 STL里面的 make_heap(), pop_he ...

  6. C++中priority_queue的用法

    本来想自己写一写的,但看到这个随笔,感觉要写的东西跟这个差不多,就直接附上链接. 需要注意事项: rand()函数需要引入头文件#include<cstdlib>. 自定义类型,重载ope ...

  7. poj 3253 Fence Repair(模拟huffman树 + 优先队列)

    题意:如果要切断一个长度为a的木条需要花费代价a, 问要切出要求的n个木条所需的最小代价. 思路:模拟huffman树,每次选取最小的两个数加入结果,再将这两个数的和加入队列. 注意priority_ ...

  8. 堆应用---构造Huffman树(C++实现)

    堆: 堆是STL中priority_queue的最高效的实现方式(关于priority_queue的用法:http://www.cnblogs.com/flyoung2008/articles/213 ...

  9. NOIP经典基础模板总结

    date: 20180820 spj: 距离NOIP还有81天 目录 STL模板: priority_queue 的用法:重载<,struct cmpqueue 的用法 stack 的用法vec ...

随机推荐

  1. NLog在.NET Core Console Apps中的简单应用

    什么是NLog? NLog is a free logging platform for .NET with rich log routing and management capabilities. ...

  2. redis配置注意事项

    最近在看redis方面的官方文档,redis-server的相关配置建议如下: 1.vm.overcommit_memory = 1 2.禁用linux内核特性transparent huge pag ...

  3. 配置文件,环境配置和war报分离,方便生产更改

    在生产环境实现配置文件和war包 的分离,为方便在必要的时候进行一定的更改,可以避免修改包,但是需要重启 最初为这样的选择配置,单不知为何未生效,修改为配置2配置方法,但不灵活,待跟进.配置1: &l ...

  4. IE浏览器中ajax使用缓存数据的问题

    今天做了一个小功能:点击鼠标实时更新系统时间,采用ajax,过程很顺利,没遇到啥差错,谷歌,火狐,欧鹏一律通过,怀着忐忑的心情点开了IE8,果然,IE要对得起前端杀手的称号:更新不了时间. 查了一下这 ...

  5. windows下mongodb安装与使用

    首先安装mongodb 1.下载地址:http://www.mongodb.org/downloads 2.解压缩到自己想要安装的目录,比如d:\mongodb 3.创建文件夹d:\mongodb\d ...

  6. Office版本问题0x80029C4A

    说来奇怪,以前运行正常的程序(涉及excel表格输出),现在运行失败了,一调试,发现了如下问题: 无法将类型为"Microsoft.Office.Interop.Excel.Applicat ...

  7. GridView自带的分页功能实现

    要实现GrdView分页的功能操作如下:1.更改GrdView控件的AllowPaging属性为true.2.更改GrdView控件的PageSize属性为 任意数值(默认为10)3.更改GrdVie ...

  8. SharePoint通过stsadm备份和还原子网站(不是网站集)

    大家都知道SharePoint的stsadm命令提供了很多便捷甚至是唯一的操作方法! 这里列出的所有命令:http://www.cnblogs.com/dadongzuo/archive/2013/0 ...

  9. 用win下的快捷键提高工作效率

    常用的快捷键: WIN+D:显示桌面,再按一次还原桌面: WIN+R:打开运行,输入命令可以执行相应操作,输入路径可以打开对应路径,输入程序名称可以打开对应程序(前提是你打开的是windows下面的程 ...

  10. iOS设计模式之观察者模式

    观察者模式 基本理解 观察者模式又叫做发布-订阅(Publish/Subscribe)模式. 观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态发生变化时 ...