std::priority_queue

template <class T, class Container = vector<T>,
class Compare = less<typename Container::value_type> > class priority_queue;

Priority queue

Priority(优先) queues are a type of container adaptors, specifically designed such that its first element is always the greatest of the elements it contains, according to some strict weak ordering criterion(严格的弱排序标准).

This context is similar to a heap, where elements can be inserted at any moment, and only the max heap element can be retrieved (the one at the top in the priority queue).

Priority queues are implemented as container adaptors, which are classes that use an encapsulated(封装) object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are popped from the "back" of the specific container, which is known as the top of the priority queue.

The underlying container may be any of the standard container class templates or some other specifically designed container class. The container shall be accessible through random access iterators and support the following operations:

  • empty()
  • size()
  • front()
  • push_back()
  • pop_back()

The standard container classes vector and deque fulfill these requirements. By default, if no container class is specified for a particular priority_queue class instantiation, the standard container vector is used.

Support of random access iterators is required to keep a heap structure internally at all times. This is done automatically by the container adaptor by automatically calling the algorithm functions make_heap, push_heap and pop_heap when needed.

Template parameters

  • T Type of the elements. Aliased as member type priority_queue::value_type.
  • Container Type of the internal underlying container object where the elements are stored. Its value_type shall be T. Aliased as member type priority_queue::container_type.
  • Compare A binary predicate that takes two elements (of type T) as arguments and returns a bool. The expression comp(a,b), where comp is an object of this type and a and b are elements in the container, shall return true if a is considered to go before b in the strict weak ordering the function defines. The priority_queue uses this function to maintain the elements sorted in a way that preserves heap properties (i.e., that the element popped is the last according to this strict weak ordering). This can be a function pointer or a function object, and defaults to less, which returns the same as applying the less-than operator (a<b).

Member types

member type definition notes
value_type The first template parameter (T) Type of the elements
container_type The second template parameter (Container) Type of the underlying container
size_type an unsigned integral type usually the same as size_t

Member functions

  • (constructor) Construct priority queue (public member function )
  • empty Test whether container is empty (public member function )
  • size Return size (public member function )
  • top Access top element (public member function )
  • push Insert element (public member function )
  • emplace Construct and insert element (public member function )
  • pop Remove top element (public member function )
  • swap Swap contents (public member function )

Non-member function overloads

  • swap (queue): Exchange contents of priority queues (public member function )

Non-member class specializations

  • uses_allocator: Uses allocator for priority queue (class template )

Code Example

#include <iostream>
#include <vector>
#include <queue>
#include <functional> using namespace std; class comparison
{
bool reverse; public:
comparison( const bool& revparam=false )
{ reverse = revparam; } bool operator() (const int& lhs, const int& rhs) const
{
if (reverse) return (lhs>rhs);
else return (lhs < rhs);
}
}; int main(int argc, char **argv)
{
int intArr[] = {10, 60, 50, 20}; priority_queue<int> first;
priority_queue<int> second( intArr, intArr + 4 );
priority_queue<int, vector<int>, greater<int> > third(intArr, intArr+4); typedef priority_queue<int, vector<int>, comparison> pq_type; pq_type fourth; ///< less than comparison
pq_type fifth( comparison(true) ); ///< greater than comparison /**
* The example does not produce any output, but it constructs different
* priority_queue objects:- First is empty.- Second contains the four
* ints defined for myints, with 60 (the highest) at its top.- Third has
* the same four ints, but because it uses greater instead of the
* default (which is less), it has 10 as its top element.- Fourth and
* fifth are very similar to first: they are both empty, except that these
* use mycomparison for comparisons, which is a special stateful
* comparison function that behaves differently depending on a flag set
* on construction.
* */ priority_queue<int> pq;
pq.push(10);
pq.push(20);
pq.push(30);
pq.push(5);
cout << "pq.top() is :" << pq.top() << '\n';
return 0;
}

Reference

cplusplus


C++ std::priority_queue的更多相关文章

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

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

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

  3. STL--容器适配器(queue、priority_queue、stack)

    适配器(Adaptor)是提供接口映射的模板类.适配器基于其他类来实现新的功能,成员函数可以被添加.隐藏,也可合并以得到新的功能. STL提供了三个容器适配器:queue.priority_queue ...

  4. 容器适配器之priority_queue

    template <class T, class Container = vector<T>,                class Compare = less<type ...

  5. [C/C++标准库]_[0基础]_[优先队列priority_queue的使用]

    std::priority_queue 场景: 1. 对于一个任务队列,任务的优先级由任务的priority属性指明,这时候就须要优先级越高的先运行.而queue并没有排序功能,这时priority_ ...

  6. 【STL】c++ priority_queue的使用方法

    最开始在项目文档看到priority_queue这个模板时,还以为是自己定义的呢,后来查了一下,原来这是STL中存在的一种优先队列. 1.最简单的使用方法 std::priority_queue< ...

  7. 浅谈C++ STL中的优先队列(priority_queue)

    从我以前的博文能看出来,我是一个队列爱好者,很多并不是一定需要用队列实现的算法我也会采用队列实现,主要是由于队列和人的直觉思维的一致性导致的. 今天讲一讲优先队列(priority_queue),实际 ...

  8. 让priority_queue支持小根堆的几种方法

    点击这里了解什么是priority_queue 前言 priority_queue默认是大根堆,也就是大的元素会放在前面 例如 #include<iostream> #include< ...

  9. C++标准模板库(STL)之Priority_Queue

    1.Priority_Queue的常用用法 priority_queue:优先队列,底层是使用堆来实现的.优先队列中,队首元素一定是当前队列中优先级最高的哪一个. a (优先级3),b(优先级4),c ...

随机推荐

  1. 谈谈DOMContentLoaded:Javascript中的domReady引入机制

    一.扯淡部分 回想当年,在摆脱写页面时js全靠从各种DEMO中copy出来然后东拼西凑的幽暗岁月之后,毅然决然地打算放弃这种处处“拿来主义”的不正之风,然后开启通往高大上的“前端攻城狮”的飞升之旅.想 ...

  2. 移动web基本知识

    1.pixel像素基础 1.px:csspixel 逻辑像素,浏览器所使用的抽象单位 2.dp,pt:设备无关像素 3.devicePixelPatio 设备像素缩放比例 2.viewport 1. ...

  3. ABP文档 - SignalR 集成

    文档目录 本节内容: 简介 安装 服务端 客户端 连接确立 内置功能 通知 在线客户端 帕斯卡 vs 骆峰式 你的SignalR代码 简介 使用Abp.Web.SignalR nuget包,使基于应用 ...

  4. div实现自适应高度的textarea,实现angular双向绑定

    相信不少同学模拟过腾讯的QQ做一个聊天应用,至少我是其中一个. 过程中我遇到的一个问题就是QQ输入框,自适应高度,最高高度为3row. 如果你也像我一样打算使用textarea,那么很抱歉,你一开始就 ...

  5. Oracle安装部署,版本升级,应用补丁快速参考

    一.Oracle安装部署 1.1 单机环境 1.2 Oracle RAC环境 1.3 Oracle DataGuard环境 1.4 主机双机 1.5 客户端部署 二.Oracle版本升级 2.1 单机 ...

  6. C#文件安全管理解析

    在实际的项目开发中,我们经常需要使用到文件的I/O操作,主要包含对文件的增改删查等操作,这些基本的操作我们都是很熟悉,但是较少的人去考虑文件的安全和操作的管理等方面,例如文件的访问权限管理,文件数据的 ...

  7. [内核笔记1]内核文件结构与缓存——inode和对应描述

    由来:公司内部外网记录日志的方式现在都是通过Nginx模块收到数据发送到系统消息队列,然后由另外一个进程来从消息队列读取然后写回磁盘这样的操作,尽量的减少Nginx的阻塞. 但是由于System/V消 ...

  8. iptables

    一.在服务器上打开 22.80.9011端口: iptables -A INPUT -p tcp --dport 9011 -j ACCEPT iptables -A OUTPUT -p tcp -- ...

  9. 开发者最爱的Firebug停止更新和维护

        近日,Firebug团队在其官网上宣布,Firebug将不再继续开发和维护,并邀请大家使用Firefox的内置开发工具.     Firebug最初是2006年1月由Joe Hewitt编写, ...

  10. java设计模式之单例模式(几种写法及比较)

    概念: Java中单例模式是一种常见的设计模式,单例模式的写法有好几种,这里主要介绍三种:懒汉式单例.饿汉式单例.登记式单例. 单例模式有以下特点: 1.单例类只能有一个实例. 2.单例类必须自己创建 ...