【转】priority_queue的用法
http://www.cnblogs.com/flyoung2008/articles/2136485.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; } |
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; } |
#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; } |
但此时不能像基本类型这样声明
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的用法的更多相关文章
- STL priority_queue 常见用法详解
<算法笔记>学习笔记 priority_queue 常见用法详解 //priority_queue又称优先队列,其底层时用堆来实现的. //在优先队列中,队首元素一定是当前队列中优先级最高 ...
- priority_queue 优先队列用法
//采用默认优先关系: //(priority_queue<int>que;) //Queue 0: // 91 83 72 56 47 36 22 14 10 7 3 // //采用结构 ...
- (转)priority_queue的用法
priority_queue调用 STL里面的 make_heap(), pop_heap(), push_heap() 算法实现,也算是堆的另外一种形式.先写一个用 STL 里面堆算法实现的与真正的 ...
- 【STL】-priority_queue的用法
初始化: priority_queue<int> maxPQ; priority_queue<int,vector<int& ...
- priority_queue的用法
priority_queue本质是一个堆. 1. 头文件是#include<queue> 2. 关于priority_queue中元素的比较 模板申明带3个参数:priority_queu ...
- C++中priority_queue的用法
本来想自己写一写的,但看到这个随笔,感觉要写的东西跟这个差不多,就直接附上链接. 需要注意事项: rand()函数需要引入头文件#include<cstdlib>. 自定义类型,重载ope ...
- poj 3253 Fence Repair(模拟huffman树 + 优先队列)
题意:如果要切断一个长度为a的木条需要花费代价a, 问要切出要求的n个木条所需的最小代价. 思路:模拟huffman树,每次选取最小的两个数加入结果,再将这两个数的和加入队列. 注意priority_ ...
- 堆应用---构造Huffman树(C++实现)
堆: 堆是STL中priority_queue的最高效的实现方式(关于priority_queue的用法:http://www.cnblogs.com/flyoung2008/articles/213 ...
- NOIP经典基础模板总结
date: 20180820 spj: 距离NOIP还有81天 目录 STL模板: priority_queue 的用法:重载<,struct cmpqueue 的用法 stack 的用法vec ...
随机推荐
- C 字符/字符串经常使用函数
string.h中经常使用函数 char * strchr(char * str ,char ch); 从字符串str中查找首次出现字符ch的位置,若存在返回查找后的地址.若不存在则返回NULL vo ...
- GraphX 图数据建模和存储
背景 简单分析一下GraphX是怎么为图数据建模和存储的. 入口 能够看GraphLoader的函数. def edgeListFile( sc: SparkContext, path: String ...
- perl模块 Compress::Raw::Lzma 的安装
perl模块 Compress::Raw::Lzma 的安装 用 cpan 安装任意perl模块总是提示 Couldn't untar Compress-Raw-Lzma-2.070.tar: 'Ca ...
- actionbar spinner-用法实例
今天需要更改一个actionbar上的spinner的字体颜色,结果试了好长时间都没有解决,最后才发现,原来他是在代码下增加的一个textview,然后使用adapter加载的,并不是直接用frame ...
- HDU1969 Pie(二分搜索)
题目大意是要办生日Party,有n个馅饼,有f个朋友.接下来是n个馅饼的半径.然后是分馅饼了, 注意咯自己也要,大家都要一样大,形状没什么要求,但都要是一整块的那种,也就是说不能从两个饼中 各割一小块 ...
- DB2 概览
2006:IBM公布DB2.9.将数据库领域带入XML时代.IT建设业已进入SOA(Service-Oriented Architecture)时代.实现SOA.其核心难点是顺畅解决不同应用间的数据交 ...
- 短网址ShortUrl的算法
场景: 我们在新浪微博上公布网址的时候.微博会自己主动判别网址.并将其转换.比如:http://t.cn/hrYnr0. 为什么要这样做的,原因我想有这样几点: 1.微博限制字数为140字一条,那么假 ...
- 从反编译深入理解JAVA内部类类结构以及finalkeyword
1.为什么成员内部类能够无条件訪问外部类的成员? 在此之前,我们已经讨论过了成员内部类能够无条件訪问外部类的成员,那详细到底是怎样实现的呢?以下通过反编译字节码文件看看到底.其实,编译器在进行编译的时 ...
- MVC+EasyUI实现查询显示到对应表格
这里要说的显示界面是Razor页面.我们要使用easyui首先应该在界面中加入对应的引用,例如以下代码,这些都是必要的引用文件,能够依据自己所存放的路径来加入src地址. @*加入Jquery Eas ...
- apache与IIS共用80端口冲突解决方法
如果同一台电脑安装了apache和iis,会提示80端口冲突,如何解决apache与iis 80端口冲突的问题呢,并且同时使用apache和iis 将apache设为使用80端口,IIS使用其它端口, ...