priority_queue实现
#include <algorithm> using namespace std;
/*
priority_queue只允许在底端加入元素,并从顶端取出元素,
其内部元素不是依照被推入的次序排列,而是自动按照元素的权值排列,权值最大的元素排在最前面
缺省情况下priority_queue是利用一个max_heap完成.模板默认参数是vector,less.
常用接口:top(), push(), pop(),类似栈的接口
*/
template <class T,class Sequence=vector<T>,class Compare=less<typename Sequence::value_type>>
class priority_queue
{
public:
typedef typename Sequence::value_type value_type;
typedef typename Sequence::size_type size_type;
typedef typename Sequence::reference reference;
typedef typename Sequence::const_reference const_reference;
protected:
Sequence c;//底层容器
Compare comp;//元素大小比较标准
public:
priority_queue() :c(){}
explicit priority_queue(const Compare& x) :c(), comp(x){} //make_heap(),push_heap(),pop_heap()都是泛型算法
template <class InputIterator>
priority_queue(InputIterator first, InputIterator last, const Compare& x) : c(first, last), comp(x)
{
make_heap(c.begin(), c.end(), comp);
}
template <class InputIterator>
priority_queue(InputIterator first, InputIterator last) : c(first,last)
{
make_heap(c.begin(), c.end(), comp);
} void push(const value_type &x)
{
__STL_TRY{
c.push_back(x);
//重排heap
push_heap(c.begin(), c.end(), comp);
}
__STL_UNWIND(c.clear());
} void pop()
{
__STL_TRY{
pop_heap(c.begin(), c.end(), comp);
c.pop_back();
}
__STL_UNWIND(c.clear());
}
};
priority_queue实现的更多相关文章
- C++ std::priority_queue
std::priority_queue template <class T, class Container = vector<T>, class Compare = less< ...
- 【转载】STL之priority_queue
参考资料:传送门先回顾队列的定义:队列(queue)维护了一组对象,进入队列的对象被放置在尾部,下一个被取出的元素则取自队列的首部.priority_queue特别之处在于,允许用户为队列中存储的元素 ...
- STL之priority_queue
下面以 long long 型队列介绍: Q.empty() // 判断队列是否为空 返回ture表示空 返回false表示空 bool Q.top() // 返回顶端元素的值 元素还在队列里 lon ...
- 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动
一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...
- STL之容器适配器priority_queue
priority_queue(优先队列)是一个拥有权值观念的queue,它允许加入新元素,删除旧元素,审视元素值等功能.由于这是一个queue,所以只允许在底端加入元素,并从顶端取出元素, 除此之外别 ...
- priority_queue 示例
http://www.cplusplus.com/reference/queue/priority_queue/ priority_queue 的top始终保持着为一堆数据中的最大元素. 读取最小 O ...
- 优先队列priority_queue的比较函数
STL头文件:#include<queue> 优先队列: 默认从大到小排列:priority_queuee<node>q; 自定义优先级的三种方法: 1.重载操作符: bool ...
- 5.1 stack,queue以及priority_queue
*:stack 使用要包含头文件stack,栈是一种先进后出的元素序列,删除和访问只能对栈顶的元素(最后一个添加的元素)进行,并且添加元素只能添加到栈顶.栈内的元素不能访问,要想访问先要删除其上方的所 ...
- hdu 1053 (huffman coding, greedy algorithm, std::partition, std::priority_queue ) 分类: hdoj 2015-06-18 19:11 22人阅读 评论(0) 收藏
huffman coding, greedy algorithm. std::priority_queue, std::partition, when i use the three commente ...
- priority_queue 优先队列用法
//采用默认优先关系: //(priority_queue<int>que;) //Queue 0: // 91 83 72 56 47 36 22 14 10 7 3 // //采用结构 ...
随机推荐
- 1、DOS基本命令
命令dir能给列出当前目录下面的所有文件.程序和子目录.所有目录(Windows 中称为文件夹)的目录名前面都有一个<DIR>标记.文件和程序名前面显示有这些文件和程序的大小. 想说的是, ...
- struts2之actionSupport学习
actionSupport在手工完成字段验证,显示错误消息,国际化等情况下推荐使用.
- ThinkPHP系统流程
1.用户通过入口文件访问控制器2.控制器从模型层中提取数据3.控制器将数据返回模板页面
- 【译】x86程序员手册26-7.5任务切换
7.5 Task Switching 任务切换 The 80386 switches execution to another task in any of four cases: 80386在以下四 ...
- 北大ACM(POJ1018-Communication System)
Question:http://poj.org/problem?id=1018 问题点:枚举. Memory: 564K Time: 329MS Language: C++ Result: Accep ...
- Tcl之Read files for synthesis
The following file is to read all design files into syntehsis tool automatically, like Cadence RTL C ...
- Hibernate框架之HQL查询与Criteria 查询的区别
Hibernate框架提供了HQL查询和Criteria 查询.下面对这两种查询分别做个例子.也好对这两种查询方法有个大概的了解.就用房屋信息表做例子,查询所有房屋信息. HQL语句查询所有房屋信息: ...
- Jmeter之关联——常用提取器
Jmeter关联 所谓关联,从业务角度讲,即:某些操作步骤与其相邻步骤存在一定的依赖关系,导致某个步骤的输入数据来源于上一步的返回数据,这时就需要“关联”来建立步骤之间的联系. 简单来说,就是:将上一 ...
- jmeter普通的接口测试
摘自:后知者.https://www.cnblogs.com/houzhizhe/p/6838731.html jmeter可以做接口测试,其中接口测试的简单操作包括做http脚本(发get/post ...
- vue基础---实例
(1)数据和方法 ①响应式双向绑定 当一个 Vue 实例被创建时,它向 Vue 的响应式系统中加入了其 data 对象中能找到的所有的属性.当这些属性的值发生改变时,视图将会产生“响应”,即匹配更新为 ...