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. 创建 OVS flat network - 每天5分钟玩转 OpenStack(134)

    上一节完成了 flat 的配置工作,今天创建 OVS flat network.Admin -> Networks,点击 "Create Network" 按钮. 显示创建页 ...

  2. 【调侃】IOC前世今生

    前些天,参与了公司内部小组的一次技术交流,主要是针对<IOC与AOP>,本着学而时习之的态度及积极分享的精神,我就结合一个小故事来初浅地剖析一下我眼中的“IOC前世今生”,以方便初学者能更 ...

  3. static,你还敢用吗?(二)

    为了压系统,昨天小组在测试环境模拟了一大批订单数据.今天上午查看记录的账单计息日志,发现了一大堆的MySqlException MySql.Data.MySqlClient.MySqlExceptio ...

  4. hash表长度优化证明

    hash表冲突的解决方法一般有两个方向: 一个是倾向于空间换时间,使用向量加链表可以最大程度的在节省空间的前提下解决冲突. 另外一个倾向于时间换空间,下面是关于这种思路的一种合适表长度的证明过程: 这 ...

  5. android键盘

    在应用的开发过程中有不少的情况下会用到自定义键盘,例如支付宝的支付密码的输入,以及类似的场景.android系统给开发者们提供了系统键盘,KeyboardView,其实并不复杂,只是有些开发者不知道罢 ...

  6. angular2系列教程(八)In-memory web api、HTTP服务、依赖注入、Observable

    大家好,今天我们要讲是angular2的http功能模块,这个功能模块的代码不在angular2里面,需要我们另外引入: index.html <script src="lib/htt ...

  7. CommandPattern

    /** * 命令模式 * @author TMAC-J * 将调用者和接受者分离 * 可以将一组命令组合在一起,适合很多命令的时候 */ public class CommandPattern { i ...

  8. css知多少之绝对定位小记

    一.position定位常见属性 对于属性position来说,属性值有static/relative/absolute/fixed/inherit以下只对绝对定位position:absolute详 ...

  9. css样式之border-radius

    border-radius 属性设置边框的园角 可能的值:像素,百分比 扩展延伸 html代码 <div></div> css代码 div { height: 200px; w ...

  10. git命令行操作

    从本地上传代码到仓库(假设已经建好仓库): 1.初始化: git init 2.将所有文件加入缓存区: git add * 3.提交当前工作空间的修改内容: git commit -m 'commit ...