从网上搜优先级队列用法,都是有些乱七八糟的,有几种用法都没说,直接贴代码。实在郁闷,于是自己在此归纳归纳。

废话不多说,直入主题。

优先级队列的核心是比较函数的实现。

比较函数有两种实现方法:

1、在结构体或类外面定义一个比较结构体。  //假如有个Point结构体。则new对象的时候:priority_queue<Point,vector<Point>,cmp> pg;其中cmp是自定义比较函数

2、在结构体或类中自己重载<操作符。   //假如有个Point结构体。这种方式定义优先级队列: priority_queue<Point> pg;

第1种方法实现代码:

  1. #include <iostream>
  2. #include <queue>
  3. #include <algorithm>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. class Point
  9. {
  10. public:
  11. int x,y;
  12. };
  13.  
  14. struct cmp
  15. {
  16. bool operator()(Point a,Point b)
  17. {
  18. return a.x>b.x; //返回 !cmp
  19. }
  20. };
  21.  
  22. priority_queue<Point,vector<Point>,cmp> pq;
  23. Point p;
  24.  
  25. int main()
  26. {
  27. int n;
  28. while(cin>>n)
  29. {
  30. while(!pq.empty()) pq.pop();
  31. while(n--)
  32. {
  33. cin>>p.x>>p.y;
  34. pq.push(p);
  35. }
  36. while(!pq.empty())
  37. {
  38. cout<<pq.top().x<<"-"<<pq.top().y<<" ";
  39. pq.pop();
  40. }
  41. }
  42. return 0;
  43. }

  

第2种方法实现代码:

  1. #include <iostream>
  2. #include <queue>
  3. #include <algorithm>
  4. #include <functional>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. class Point
  10. {
  11. public:
  12. friend bool operator <(Point a,Point b);
  13. int x,y;
  14. };
  15.  
  16. //友元函数在外面实现 也可在类里面实现
  17. bool operator <(Point a,Point b) //优先级队列要求必须要实现<的重载,否则编译错误 而int型有默认的<函数。
  18. {
  19. return a.x>b.x; //返回比较结果的相反值,这种情况是从小到大排序
  20. }
  21.  
  22. /** 友元函数在类里面实现如下
  23. class Point
  24. {
  25. public:
  26. friend bool operator <(Point a,Point b)
  27. {
  28. return a.x>b.x;
  29. }
  30. int x,y;
  31. };
  32. **/
  33. priority_queue<Point> pq;
  34. Point p;
  35.  
  36. int main()
  37. {
  38. int n;
  39. while(cin>>n)
  40. {
  41. while(!pq.empty()) pq.pop();
  42. while(n--)
  43. {
  44. cin>>p.x>>p.y;
  45. pq.push(p);
  46. }
  47. while(!pq.empty())
  48. {
  49. cout<<pq.top().x<<"-"<<pq.top().y<<" ";
  50. pq.pop();
  51. }
  52. }
  53. return 0;
  54. }

  

c++ 优先级队列(priority_queue)的更多相关文章

  1. STL学习系列七:优先级队列priority_queue容器

    1.简介 最大值优先级队列.最小值优先级队列 优先级队列适配器 STL priority_queue 用来开发一些特殊的应用,请对stl的类库,多做扩展性学习 这里给个例子: #include< ...

  2. C++ STL 学习笔记__(6)优先级队列priority_queue基本操作

    10.2.7优先级队列priority_queue v  最大值优先级队列.最小值优先级队列 v  优先级队列适配器 STL priority_queue v  用来开发一些特殊的应用,请对stl的类 ...

  3. C++ - 库函数优先级队列(priority_queue)输出最小值 代码

    库函数优先级队列(priority_queue)输出最小值 代码 本文地址: http://blog.csdn.net/caroline_wendy 库函数优先级队列(priority_queue)的 ...

  4. STL之优先级队列priority_queue

    摘要: priority_queue,自适应容器(即容器适配器):不能由list来组建: 最大值优先级队列(最大值始终在对首,push进去时候) 最小值优先级队列: 优先级队列适配器 STL  pri ...

  5. STL中的优先级队列priority_queue

    priority_queue(queue类似)完全以底部容器为根据,再加上二叉堆(大根堆或者小根堆)的实现原理,所以其实现非常简单,缺省情况下priority_queue以vector作为底部容器.另 ...

  6. STL中的优先级队列(priority_queue)的自己实现priqueue

    这篇文章主要介绍堆(最大堆和最小堆),以及一些系统对一些任务,比如线程,进程做调度的时候,所采用的优先级队列. 主要思想就是,做一个最大堆(任务的权重最大的在顶端),把顶端的任务取出,重新做一个堆,处 ...

  7. 优先级队列Priority_queue

    定义 拥有权值观点的queue,,一个是返回最高优先级对象,一个是在底端添加新的对象.这种数据结构就是优先级队列(Priority Queue) . 实现 利用max_heap完成,以vector表现 ...

  8. STL-优先级队列-priority_queue

    头文件是<queue> 操作很简单 #include <iostream> #include <cstdio> #include <queue> usi ...

  9. cb05a_c++_STL优先级队列priority_queue_less_greater

    /*cb05a_c++_STL优先级队列priority_queue自适应容器(容器适配器):不能使用list,list不能使用随机操作最大值优先级队列,//把数据放在队列里面是,最大的始终都是放在最 ...

随机推荐

  1. 通过请求接口的办法获得本设备IP以及IP地址

    获取本设备IP接口(搜狐) http://pv.sohu.com/cityjson?ie=utf-8 result Content: {    "cip": "58.21 ...

  2. 02017_String类方法使用练习

    1.获取指定字符串中,大写字母.小写字母.数字的个数. public static void method(String str){ int bigCount = 0; //大写字母的个数 int s ...

  3. Nginx学习总结(1)——Nginx入门简介

    本文主要介绍一些Nginx的最基本功能以及简单配置,但不包括Nginx的安装部署以及实现原理.废话不多,直接开始. 1.静态HTTP服务器 首先,Nginx是一个HTTP服务器,可以将服务器上的静态文 ...

  4. Ubuntu下的Apache、Mysql、PHP环境搭建

    由于刚学习Linux,选择了界面比较友好的Ubuntu进行研究.命令行+可视化对于初学者来说组合还是比较不错的,图形界面作为命令行的一个过渡能比较直观的看到效果.在应用中学习是一个比较好的办法,我就是 ...

  5. JQuery学习(5-AJAX1)

    <? php /* * 保护AJAX请求的方式 */ /* * 1. 防止表单的自己主动提交 * 对表单的submit提交进行控制. 设置一个全局变量submitError,在进行验证的方法体中 ...

  6. POJ 3076

    DLX算法,刚接触,是关于精确覆盖的,白书上有算法介绍. 代码模板 #include <iostream> #include <cstdio> #include <cst ...

  7. 使用imgareaselect 辅助后台进行图片裁剪

    由于项目其中用到图片裁剪,本来能够不用到后台进行裁剪的,可是要兼容万恶的IE浏览器,所以不得不使用后台进行裁剪. 这次使用到imgareaselect 插件获取须要裁剪区域的坐标.再由后台进行裁剪操作 ...

  8. bzoj1081: [SCOI2005]超级格雷码(dfs)

    1081: [SCOI2005]超级格雷码 题目:传送门 题解: 又是一道水题... 因为之前做过所以知道规律: 如n=2 B=3: 00 10 20    21 11 01    02 12 22 ...

  9. Linux - 配置php-fpm 以及 配置nginx支持php

    配置php-fpm [root@localhost php7]# which php-fpm /usr/local/php7/sbin/php-fpm [root@localhost php7]# p ...

  10. xenserver&nbsp;增加新硬盘

    xenserver 增加新硬盘 1.XS创建本地存储 首先 分区好的的硬盘接到服务器上 查看所有硬盘了的id ls -l /dev/disk/by-id/ 记下硬盘的全称.接下来开始挂载  xe sr ...