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

废话不多说,直入主题。

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

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

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)的更多相关文章

  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. 2.1 SVN的安装

     一.SVN客户端安装 运行TortoiseSVN-1.6.6.17493-win32-svn-1.6.6.msi程序, 开始安装 点击Next, 下一步 选择 I accept 接受, 点击Next ...

  2. Game with a Strip

    Game with a Strip Time limit: 2.0 secondMemory limit: 64 MB There is a strip 1 × n with two sides. E ...

  3. hibernate反向生成

    1.在Myeclipse Database Explore中创建一个库的链接. 2.在Myeclipse中创建一个web工程,添加反向生成相关的引用.即在项目上右键,弹出Myeclipse选择add  ...

  4. LeetCode之RemoveElement

    题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...

  5. HDU 5187

    超简单的公式题(2^n-2).不过,要过可不容易,因为会爆64位,所以,可以使用快速乘法. #include <iostream> #include <cstdio> #inc ...

  6. NEFU 115

    刚开始,做了水题 #include <iostream> #include <cstdio> #include <algorithm> using namespac ...

  7. tomcat下载及启动

    http://tomcat.apache.org/ 打开网页,在左边选择版本,选择后网页往下面拉 拉下来,根据windows选择32还是64位的,其中zip是windows免安装版 下载后解压,然后配 ...

  8. Linux命令(二)——目录和文件管理命令

    一.Linux系统的目录结构 1.根目录(/):顶层目录,某些系统中的唯一分区. 2./bin命令文件目录:包含Linux命令的二进制可执行文件. 3./boot目录:存放系统的内核文件和引导装载程序 ...

  9. 谋哥:App开发人员的苦逼不值得怜悯!!

    [谋哥每天一干货,第四十篇]         为什么取这个标题呢?由于昨天一些本来"支持"谋哥的人看到谋哥搞收费VIP群,认为谋哥赚苦逼开发人员的钱非常不道德,且说谋哥我写的东西都 ...

  10. 二重积分的计算 —— 交换积分顺序(exchange the order of integration)

    交换积分顺序的诀窍在数形结合: 1. 几句顺口溜 后积先定限,限内穿条线,先交下限写,后交上限见 先积 x,画横线(平行于 x 轴),右减左: 先积 y,画竖线(平行于 y 轴),上减下: 2. 简单 ...