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不能使用随机操作最大值优先级队列,//把数据放在队列里面是,最大的始终都是放在最 ...
随机推荐
- javascript正则表达式总结(test|match|search|replace|split|exec)
test:测试string是否包含有匹配结果,包含返回true,不包含返回false. <script type="text/javascript"> var str ...
- PAT 1065. A+B and C
Given three integers A, B and C in [-263, 263], you are supposed to tell whether A+B > C. Input S ...
- Top English interview Q&A part 2.
https://www.zhihu.com/question/19666878 1.how do you handle failure? I have always lived by the maxi ...
- html绑定点击事件传值
1.方法1function radioSelect(event){ var valueRadio=event.value; if(valueRadio =='目的口岸'){ return '目的口岸' ...
- 合并目录中的txt文件
txt 文件 合并 并 按章节 分隔 # -*- coding: utf8 -*- import os result_name = 'result.txt' def resplit_txt_in_fo ...
- elasticsearch Suggester实现搜索建议(八)
Completion Suggester 智能提示 { "settings": { }, "mappings": { "doc": { &q ...
- MySQL用Load Data local infile 导入部分数据后中文乱码
今天在两台MySQL服务器之间导数据,因为另一个MySQL服务器是测试用的,差一个月的数据,从现有MySQL服务器select到一个文件,具体语句是: select * from news where ...
- MEAN框架介绍
近期在Angular社区的原型开发人员间.一种全Javascript的开发架构MEAN正突然流行起来.其首字母分别代表的是:(M)ongoDB--NoSQL的文档数据库,使用JSON风格来存储数据,甚 ...
- leetCode 74.Search a 2D Matrix(搜索二维矩阵) 解题思路和方法
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- ASP.NET MVC2 Web项目中公用类库的问题
ASP.NET WEB窗体 网站中,加入公用类文件的话,系统会很自动并殷勤的问你,说要不要把它存放在文件夹 App_Code 里.一旦加入,全站都可以很方便地加以使用,一点问题没有. 这种习以为常的方 ...