#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实现的更多相关文章

  1. C++ std::priority_queue

    std::priority_queue template <class T, class Container = vector<T>, class Compare = less< ...

  2. 【转载】STL之priority_queue

    参考资料:传送门先回顾队列的定义:队列(queue)维护了一组对象,进入队列的对象被放置在尾部,下一个被取出的元素则取自队列的首部.priority_queue特别之处在于,允许用户为队列中存储的元素 ...

  3. STL之priority_queue

    下面以 long long 型队列介绍: Q.empty() // 判断队列是否为空 返回ture表示空 返回false表示空 bool Q.top() // 返回顶端元素的值 元素还在队列里 lon ...

  4. 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动

    一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...

  5. STL之容器适配器priority_queue

    priority_queue(优先队列)是一个拥有权值观念的queue,它允许加入新元素,删除旧元素,审视元素值等功能.由于这是一个queue,所以只允许在底端加入元素,并从顶端取出元素, 除此之外别 ...

  6. priority_queue 示例

    http://www.cplusplus.com/reference/queue/priority_queue/ priority_queue 的top始终保持着为一堆数据中的最大元素. 读取最小 O ...

  7. 优先队列priority_queue的比较函数

    STL头文件:#include<queue> 优先队列: 默认从大到小排列:priority_queuee<node>q; 自定义优先级的三种方法: 1.重载操作符: bool ...

  8. 5.1 stack,queue以及priority_queue

    *:stack 使用要包含头文件stack,栈是一种先进后出的元素序列,删除和访问只能对栈顶的元素(最后一个添加的元素)进行,并且添加元素只能添加到栈顶.栈内的元素不能访问,要想访问先要删除其上方的所 ...

  9. 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 ...

  10. priority_queue 优先队列用法

    //采用默认优先关系: //(priority_queue<int>que;) //Queue 0: // 91 83 72 56 47 36 22 14 10 7 3 // //采用结构 ...

随机推荐

  1. js操作Attribute,控件的各种属性.....maxlength,style...

    Attribute是属性的意思,文章仅对部分兼容IE和FF的Attribute相关的介绍. attributes:获取一个属性作为对象 getAttribute:获取某一个属性的值setAttribu ...

  2. 《从Paxos到ZooKeeper 分布式一致性原理与实践》阅读【Leader选举】

    从3.4.0版本开始,zookeeper废弃了0.1.2这3种Leader选举算法,只保留了TCP版本的FastLeaderElection选举算法. 当ZooKeeper集群中的一台服务器出现以下两 ...

  3. duilib属性

    原文转载自:http://blog.csdn.net/lixiang987654321/article/details/45008441 这里我想讲解一下duilib中的一些属性的理解,当然这是一篇永 ...

  4. LN : leetcode 513 Find Bottom Left Tree Value

    lc 513 Find Bottom Left Tree Value 513 Find Bottom Left Tree Value Given a binary tree, find the lef ...

  5. 重新学习Java——对象和类(二)

    上一节回归了如何以面向对象的思想去使用一些Java中的公共类,也设计了一些自己的类并介绍了设计类的基本方法和技巧,这一节我们将继续回顾这些内容,并争取从中获得新的体验和感受. 1. 静态域与静态方法 ...

  6. responsive-navigator

    html 代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  7. python自动化--语言基础二运算符、格式化输出、条件语句、循环语句、列表、元组

    运算符包括:算术运算符.比较运算符.赋值运算符.逻辑运算符.成员运算符.身份运算符. 算术运算符 %   取模(余数) //  取相除的整数部分 /   (5/2=2.5) 比较运算符 ==  等于 ...

  8. dubbo-monitor安装及配置过程

    安装 1. 使用git下载(git clone https://github.com/alibaba/dubbo.git)或者从http://dubbo.io/下载源码 2. cd到dubbo的根目录 ...

  9. JsTree中节点添加CheckBox 以及 单选的实现

    stree中添加checkbox,需要在初始化时设置plugins属性: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 $('#DpTree').data('jstree', fa ...

  10. 机器学习_K近邻Python代码详解

    k近邻优点:精度高.对异常值不敏感.无数据输入假定:k近邻缺点:计算复杂度高.空间复杂度高 import numpy as npimport operatorfrom os import listdi ...