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. MVVM框架从WPF移植到UWP遇到的问题和解决方法

    MVVM框架从WPF移植到UWP遇到的问题和解决方法 0x00 起因 这几天开始学习UWP了,之前有WPF经验,所以总体感觉还可以,看了一些基础概念和主题,写了几个测试程序,突然想起来了前一段时间在W ...

  2. 【绝对干货】仿微信QQ设置图形头像裁剪,让你的App从此炫起来~

    最近在做毕业设计,想有一个功能和QQ一样可以裁剪头像并设置圆形头像,额,这是设计狮的一种潮流. 而纵观现在主流的APP,只要有用户系统这个功能,这个需求一般都是在(bu)劫(de)难(bu)逃(xue ...

  3. CSS 3学习——边框

    在CSS 3中可以设置边框圆角.边框阴影和边框图像,分别通过border-radius.border-image和box-shadow属性设置. 边框圆角 border-radius属性是以下4个属性 ...

  4. Hibernate中事务的隔离级别设置

    Hibernate中事务的隔离级别,如下方法分别为1/2/4/8. 在Hibernate配置文件中设置,设置代码如下

  5. bzoj3095--数学题

    题目大意:给定一个长度为n的整数序列x[i],确定一个二元组(b, k)使得S=Σ(k*i+b- x[i])^2(i∈[0,n-1])最小 看Claris大神的题解就行了.实际上就是用2次二次函数的性 ...

  6. git命令行操作

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

  7. 关于sql server 2005存储过程的写法

    打开数据库的SQL Server Managerment Studio---->数据库----->打开数据库会看见"可编程行"------->打开有存储过程--- ...

  8. mysql5.x升级至mysql5.7后导入之前数据库date出错的解决方法!

    mysql5.x升级至mysql5.7后导入之前数据库date出错的解决方法! 修改mysql5.7的配置文件即可解决,方法如下: linux版:找到mysql的安装路径进入默认的为/usr/shar ...

  9. python select网络编程详细介绍

    刚看了反应堆模式的原理,特意复习了socket编程,本文主要介绍python的基本socket使用和select使用,主要用于了解socket通信过程 一.socket模块 socket - Low- ...

  10. centos 7 安装mono 和 monodevelop

    本次所有操作在root模式下 1.执行  rpm --import "http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3 ...