优先队列是单向队列的一种,可以按照默认或自定义的一种方式来对队列中的数据进行动态排序

template<class _Ty, class _Container = vector<_Ty>, class _Pr = less<typename _Container::value_type> > //默认以vector为容器的
class priority_queue
{ // priority queue implemented with a _Container
public:
typedef _Container container_type;
typedef typename _Container::value_type value_type;
typedef typename _Container::size_type size_type;
typedef typename _Container::reference reference;
typedef typename _Container::const_reference const_reference; priority_queue() : c(), comp()
{ // construct with empty container, default comparator
} explicit priority_queue(const _Pr& _Pred) : c(), comp(_Pred)
{ // construct with empty container, specified comparator
} priority_queue(const _Pr& _Pred, const _Container& _Cont) : c(_Cont), comp(_Pred)
{ // construct by copying specified container, comparator
make_heap(c.begin(), c.end(), comp); //参见《STL系列之四 heap 堆的相关函数》
} template<class _Iter>
priority_queue(_Iter _First, _Iter _Last) : c(_First, _Last), comp()
{ // construct by copying [_First, _Last), default comparator
make_heap(c.begin(), c.end(), comp);
} template<class _Iter>
priority_queue(_Iter _First, _Iter _Last, const _Pr& _Pred) : c(_First, _Last), comp(_Pred)
{ // construct by copying [_First, _Last), specified comparator
make_heap(c.begin(), c.end(), comp);
} template<class _Iter>
priority_queue(_Iter _First, _Iter _Last, const _Pr& _Pred, const _Container& _Cont) : c(_Cont), comp(_Pred)
{ // construct by copying [_First, _Last), container, and comparator
c.insert(c.end(), _First, _Last);
make_heap(c.begin(), c.end(), comp);
} bool empty() const
{ // test if queue is empty
return (c.empty());
} size_type size() const
{ // return length of queue
return (c.size());
} const_reference top() const
{ // return highest-priority element
return (c.front());
} reference top()
{ // return mutable highest-priority element (retained)
return (c.front());
} void push(const value_type& _Pred)
{ // insert value in priority order
c.push_back(_Pred);
push_heap(c.begin(), c.end(), comp);
} void pop()
{ // erase highest-priority element
pop_heap(c.begin(), c.end(), comp);
c.pop_back();
} protected:
_Container c; // the underlying container
_Pr comp; // the comparator functor
};

  

用法:

1、默认用<运算符进行排序

大的先输出

2、priority_queue<type, vector<type>, fun<type>>pq;

vector<type>为容器类型

fun<type>为比较函数

3、自定义优先级

重载<

struct node
{
friend bool operator< (node n1, node n2)
{
return n1.priority < n2.priority;
}
int priority;
int value;
};

  

priority_queue 优先队列的更多相关文章

  1. 第20章 priority_queue优先队列容器

    /* 第20章 priority_queue优先队列容器 20.1 priority_queue技术原理 20.2 priority_queue应用基础 20.3 本章小结 */ // 第20章 pr ...

  2. stack堆栈容器、queue队列容器和priority_queue优先队列容器(常用的方法对比与总结)

    stack堆栈是一个后进先出的线性表,插入和删除元素都在表的一端进行. stack堆栈的使用方法: 采用push()方法将元素入栈: 采用pop()方法将元素出栈: 采用top()方法访问栈顶元素: ...

  3. priority_queue优先队列/C++

    priority_queue优先队列/C++ 概述 priority_queue是一个拥有权值观念的queue,只允许在底端加入元素,并从顶端取出元素. priority_queue带有权值观念,权值 ...

  4. (转)【C++ STL】细数C++ STL 的那些事 -- priority_queue(优先队列)

    装载自http://blog.csdn.net/tianshuai1111/article/details/7652553 一,概述 priority_queue是拥有权值观念的queue,它允许加入 ...

  5. 【转】priority_queue优先队列

    转自:http://www.cppblog.com/shyli/archive/2007/04/06/21366.html http://www.cppblog.com/shyli/archive/2 ...

  6. priority_queue(优先队列):排序不去重

    C++优先队列类似队列,但是在这个数据结构中的元素按照一定的断言排列有序. 头文件:#include<queue> 参数:priority_queue<Type, Container ...

  7. STL - priority_queue(优先队列)

    参考:http://www.cnblogs.com/xzxl/p/7266404.html 一.基本定义: 优先队列容器与队列一样,只能从队尾插入元素,从队首删除元素.但是它有一个特性,就是队列中最大 ...

  8. STL之priority_queue(优先队列)

    priority_queue是一个容器适配器,在这个容器里第一个数据元素是最大的.它的使用场景是什么样:如果12306抢票,为什么黄牛能抢这么多票,感觉12306那边的请求队列是一个优先队列,黄牛的请 ...

  9. 标准模板库(STL)学习指南之priority_queue优先队列

    转载自CSDN博客:http://blog.csdn.net/suwei19870312/article/details/5294016 priority_queue 调用 STL里面的 make_h ...

随机推荐

  1. js中substr,substring,indexOf,lastIndexOf等的用法

    1.substrsubstr(start,length)表示从start位置开始,截取length长度的字符串. var src="images/off_1.png";alert( ...

  2. HDU 1724 Ellipse 自适应simpson积分

    simpson公式是用于积分求解的比较简单的方法(有模板都简单…… 下面是simpson公式(很明显 这个公式的证明我并不会…… (盗图…… 因为一段函数基本不可能很规则 所以我们要用自适应积分的方法 ...

  3. 【转】SQL Server 2008 事件探查器(SQL SERVER Profiler)

    跟踪数据库sql语句的执行情况.例:一个系统,用到了sql server 数据库,这个系统共有500张表,当用户在前台页面做某一个操作时,比如插入,登录等等,我们想知道此刻是在对哪一张表操作,打开事件 ...

  4. EasyCHM(CHM电子书制作工具) v3.84.545 绿色版

    软件名称:EasyCHM(CHM电子书制作工具) v3.84.545 绿色版 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 2.78MB 图片预览: 软件 ...

  5. Everything(速度快的文件搜索软件) 1.4.1.801b 汉化绿色版

    软件名称: Everything(速度快的文件搜索软件) 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 2.0MB ...

  6. 提示找不到xml配置文件

    ClassPathXmlApplicationContext("applicationContext.xml")默认文件夹是resouerces,所以要把xml文件放在这个下面.

  7. python中判断语句用两个or连接的奇葩

    学python的时候犯的一个错误,放在这吧.就是在循环某个列表的时候不要去操作它,这是容易忽略的一个地方.所以如果要操作某个列表本身,那么先把该列表copy一份,然后再读取的时候读copy的那份.操作 ...

  8. vim - 自动补齐

    OmniComplete是基于ctags的,所以要先安装ctags 到http://www.vim.org/scripts/script.php?script_id=2358下载cpp_src.tar ...

  9. git 客户端提交

    01 按照git到本地 02 按照小乌龟操作面板, 03 (git 和小乌龟)自动加载到右键快捷方式

  10. Ubuntu 16.04上Docker使用手记

    一.Docker Hub的使用Docker Hub是Docker官方维护的仓库,里面已经包含了很多的镜像,一般我们的需求直接在官方仓库搜索就可以得到解决.在官方的公共仓库中我们无需登录就可以进行镜像的 ...