c++ 优先级队列(priority_queue)
从网上搜优先级队列用法,都是有些乱七八糟的,有几种用法都没说,直接贴代码。实在郁闷,于是自己在此归纳归纳。
废话不多说,直入主题。
优先级队列的核心是比较函数的实现。
比较函数有两种实现方法:
1、在结构体或类外面定义一个比较结构体。 //假如有个Point结构体。则new对象的时候:priority_queue<Point,vector<Point>,cmp> pg;其中cmp是自定义比较函数
2、在结构体或类中自己重载<操作符。 //假如有个Point结构体。这种方式定义优先级队列: priority_queue<Point> pg;
第1种方法实现代码:
- #include <iostream>
- #include <queue>
- #include <algorithm>
- #include <vector>
- using namespace std;
- class Point
- {
- public:
- int x,y;
- };
- struct cmp
- {
- bool operator()(Point a,Point b)
- {
- return a.x>b.x; //返回 !cmp
- }
- };
- priority_queue<Point,vector<Point>,cmp> pq;
- Point p;
- int main()
- {
- int n;
- while(cin>>n)
- {
- while(!pq.empty()) pq.pop();
- while(n--)
- {
- cin>>p.x>>p.y;
- pq.push(p);
- }
- while(!pq.empty())
- {
- cout<<pq.top().x<<"-"<<pq.top().y<<" ";
- pq.pop();
- }
- }
- return 0;
- }
第2种方法实现代码:
- #include <iostream>
- #include <queue>
- #include <algorithm>
- #include <functional>
- #include <vector>
- using namespace std;
- class Point
- {
- public:
- friend bool operator <(Point a,Point b);
- int x,y;
- };
- //友元函数在外面实现 也可在类里面实现
- bool operator <(Point a,Point b) //优先级队列要求必须要实现<的重载,否则编译错误 而int型有默认的<函数。
- {
- return a.x>b.x; //返回比较结果的相反值,这种情况是从小到大排序
- }
- /** 友元函数在类里面实现如下
- class Point
- {
- public:
- friend bool operator <(Point a,Point b)
- {
- return a.x>b.x;
- }
- int x,y;
- };
- **/
- priority_queue<Point> pq;
- Point p;
- int main()
- {
- int n;
- while(cin>>n)
- {
- while(!pq.empty()) pq.pop();
- while(n--)
- {
- cin>>p.x>>p.y;
- pq.push(p);
- }
- while(!pq.empty())
- {
- cout<<pq.top().x<<"-"<<pq.top().y<<" ";
- pq.pop();
- }
- }
- return 0;
- }
c++ 优先级队列(priority_queue)的更多相关文章
- STL学习系列七:优先级队列priority_queue容器
1.简介 最大值优先级队列.最小值优先级队列 优先级队列适配器 STL priority_queue 用来开发一些特殊的应用,请对stl的类库,多做扩展性学习 这里给个例子: #include< ...
- C++ STL 学习笔记__(6)优先级队列priority_queue基本操作
10.2.7优先级队列priority_queue v 最大值优先级队列.最小值优先级队列 v 优先级队列适配器 STL priority_queue v 用来开发一些特殊的应用,请对stl的类 ...
- C++ - 库函数优先级队列(priority_queue)输出最小值 代码
库函数优先级队列(priority_queue)输出最小值 代码 本文地址: http://blog.csdn.net/caroline_wendy 库函数优先级队列(priority_queue)的 ...
- STL之优先级队列priority_queue
摘要: priority_queue,自适应容器(即容器适配器):不能由list来组建: 最大值优先级队列(最大值始终在对首,push进去时候) 最小值优先级队列: 优先级队列适配器 STL pri ...
- STL中的优先级队列priority_queue
priority_queue(queue类似)完全以底部容器为根据,再加上二叉堆(大根堆或者小根堆)的实现原理,所以其实现非常简单,缺省情况下priority_queue以vector作为底部容器.另 ...
- STL中的优先级队列(priority_queue)的自己实现priqueue
这篇文章主要介绍堆(最大堆和最小堆),以及一些系统对一些任务,比如线程,进程做调度的时候,所采用的优先级队列. 主要思想就是,做一个最大堆(任务的权重最大的在顶端),把顶端的任务取出,重新做一个堆,处 ...
- 优先级队列Priority_queue
定义 拥有权值观点的queue,,一个是返回最高优先级对象,一个是在底端添加新的对象.这种数据结构就是优先级队列(Priority Queue) . 实现 利用max_heap完成,以vector表现 ...
- STL-优先级队列-priority_queue
头文件是<queue> 操作很简单 #include <iostream> #include <cstdio> #include <queue> usi ...
- cb05a_c++_STL优先级队列priority_queue_less_greater
/*cb05a_c++_STL优先级队列priority_queue自适应容器(容器适配器):不能使用list,list不能使用随机操作最大值优先级队列,//把数据放在队列里面是,最大的始终都是放在最 ...
随机推荐
- 通过请求接口的办法获得本设备IP以及IP地址
获取本设备IP接口(搜狐) http://pv.sohu.com/cityjson?ie=utf-8 result Content: { "cip": "58.21 ...
- 02017_String类方法使用练习
1.获取指定字符串中,大写字母.小写字母.数字的个数. public static void method(String str){ int bigCount = 0; //大写字母的个数 int s ...
- Nginx学习总结(1)——Nginx入门简介
本文主要介绍一些Nginx的最基本功能以及简单配置,但不包括Nginx的安装部署以及实现原理.废话不多,直接开始. 1.静态HTTP服务器 首先,Nginx是一个HTTP服务器,可以将服务器上的静态文 ...
- Ubuntu下的Apache、Mysql、PHP环境搭建
由于刚学习Linux,选择了界面比较友好的Ubuntu进行研究.命令行+可视化对于初学者来说组合还是比较不错的,图形界面作为命令行的一个过渡能比较直观的看到效果.在应用中学习是一个比较好的办法,我就是 ...
- JQuery学习(5-AJAX1)
<? php /* * 保护AJAX请求的方式 */ /* * 1. 防止表单的自己主动提交 * 对表单的submit提交进行控制. 设置一个全局变量submitError,在进行验证的方法体中 ...
- POJ 3076
DLX算法,刚接触,是关于精确覆盖的,白书上有算法介绍. 代码模板 #include <iostream> #include <cstdio> #include <cst ...
- 使用imgareaselect 辅助后台进行图片裁剪
由于项目其中用到图片裁剪,本来能够不用到后台进行裁剪的,可是要兼容万恶的IE浏览器,所以不得不使用后台进行裁剪. 这次使用到imgareaselect 插件获取须要裁剪区域的坐标.再由后台进行裁剪操作 ...
- bzoj1081: [SCOI2005]超级格雷码(dfs)
1081: [SCOI2005]超级格雷码 题目:传送门 题解: 又是一道水题... 因为之前做过所以知道规律: 如n=2 B=3: 00 10 20 21 11 01 02 12 22 ...
- Linux - 配置php-fpm 以及 配置nginx支持php
配置php-fpm [root@localhost php7]# which php-fpm /usr/local/php7/sbin/php-fpm [root@localhost php7]# p ...
- xenserver 增加新硬盘
xenserver 增加新硬盘 1.XS创建本地存储 首先 分区好的的硬盘接到服务器上 查看所有硬盘了的id ls -l /dev/disk/by-id/ 记下硬盘的全称.接下来开始挂载 xe sr ...