(转)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 ...
随机推荐
- mysql init-file参数中语句限制
mysql 启动选项中的init-file文件的内容目测只能是dml语句,不能包含ddl,否则执行就会报错,但不影响启动本身..太扯了..
- REUSE_ALV_GRID_DISPLAY显示ALV,设置可编辑时,与内表数据同步问题
使用function module: REUSE_ALV_GRID_DISPLAY显示ALV,并设置alv某些列可编辑,可是编辑后发现对应的内表数据并没有随之改变.记得需要设置一个参数的值,怎么想也记 ...
- Searching External Data in SharePoint 2010 Using Business Connectivity Services
from:http://blogs.msdn.com/b/ericwhite/archive/2010/04/28/searching-external-data-in-sharepoint-2010 ...
- 详解Paint的各种set方法
一.前言 我们用set方法来设置画笔的样式,类似于我们挑选画笔画画的过程.由于上面有些方法不支持硬件加速,所以在高版本系统中可能会没有效果.因此,我们首先来看看官方废弃的方法. 下图来自:https: ...
- 关于Eclipse 和 IDEA 导入library库文件 的步骤
这里我们以PullToRefresh(上拉刷新下拉加载)组件的library为例 下载地址: https://github.com/chrisbanes/Android-PullToRefresh 现 ...
- iOS打包ipa select a method for export几个选项的意思
他们的意思分别为:Save for iOS App Store Deployment保存到本地 准备上传App Store 或者在越狱的iOS设备上使用,需要提供发布证书 Save for Ad Ho ...
- IOS常用的系统文件目录介绍
iOS常用目录整理说明是本文要介绍的内容,虽然不同API全面,也算是在编程中常用到的存放目录,所以是必备文档,不多说,来看详细内容讲解. 1.[/Applications] 常用软件的安装目录 内建软 ...
- 编译iOS程序时的-all_load选项,以及-all_load 导致的 ld duplicate symbol xx的问题
在新的SDK环境中调试百度地图的应用程序时,app总是意外退出,找了半天发现错误的原因是unrecognized selector xx的错误,另外还有报了一个Unknown class XXX in ...
- Android线程池(一)
本篇文章主要介绍Android自带的线程池的使用. 首先要引入线程池的概念 线程池:是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务. 线程池线程都是后台线程.每个线 ...
- [android] 手机卫士设置向导页面
设置向导页面,通过SharedPreferences来判断是否已经设置过了,跳转到不同的页面 自定义样式 在res/values/styles.xml中 添加节点<style name=””&g ...