(转)priority_queue的用法
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 // //采用结构 ...
- 【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 ...
- 【转】priority_queue的用法
http://www.cnblogs.com/flyoung2008/articles/2136485.html priority_queue调用 STL里面的 make_heap(), pop_he ...
- 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 ...
随机推荐
- 如何选择RabbitMQ的消息保存方式?
RabbitMQ对于queue中的message的保存方式有两种方式:disc和ram.如果采用disc,则需要对exchange/queue/delivery mode都要设置成durable模式. ...
- Android 手机卫士16--手机杀毒
1.指定动画一直旋转 rotateAnimation.setRepeatCount(RotateAnimation.INFINITE); android:repeatCount 重复的次数,默认为0, ...
- mac下用ruby安装sass && webstorm下给scss文件添加watch
1.安装rvm 先安装 [Xcode](http://developer.apple.com/xcode/) 开发工具,它将帮你安装好 Unix 环境需要的开发包 sudo curl -L https ...
- 浅谈React受控与非受控组件
背景 React内部分别使用了props, state来区分组件的属性和状态.props用来定义组件外部传进来的属性, 属于那种经过外部定义之后, 组件内部就无法改变.而state维持组件内部的状态更 ...
- C#加密算法总结
C#加密算法总结 MD5加密 /// <summary> /// MD5加密 /// </summary> /// <param name="strPwd&qu ...
- 转:Web App开发入门
WebApp与Native App有何区别呢? Native App: 1.开发成本非常大.一般使用的开发语言为JAVA.C++.Objective-C. 2.更新体验较差.同时也比较麻烦.每一次发布 ...
- java多线程系列2-线程控制
前面的文章已经介绍了有关线程的调度,接下来介绍如何使用方法对线程进行控制 1.线程休眠 public static void sleep(long millis) /* * 线程休眠 * public ...
- iOS 删除 Main.storyboard 和 LaunchScreen.storyboard
第一步: 右键选中Main.storyboard —- delete —— Move to Trash LaunchScreen同理 第二步 点击工程名,就是最顶级目录 右侧出现general选项卡 ...
- 傅里叶:有关FFT,DFT与蝴蝶操作(转 重要!!!!重要!!!!真的很重要!!!!)
转载地址:http://blog.renren.com/share/408963653/15068964503(作者 : 徐可扬) 有没有!!! 其实我感觉这个学期算法最难最搞不懂的绝对不是动态规划 ...
- JavaScript Patterns 3.6 Regular Expression Literal
1. Using the new RegExp() constructor // constructor var re = new RegExp("\\\\", "gm& ...