C++ std::priority_queue
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
C++ std::priority_queue的更多相关文章
- 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 ...
- 第20章 priority_queue优先队列容器
/* 第20章 priority_queue优先队列容器 20.1 priority_queue技术原理 20.2 priority_queue应用基础 20.3 本章小结 */ // 第20章 pr ...
- STL--容器适配器(queue、priority_queue、stack)
适配器(Adaptor)是提供接口映射的模板类.适配器基于其他类来实现新的功能,成员函数可以被添加.隐藏,也可合并以得到新的功能. STL提供了三个容器适配器:queue.priority_queue ...
- 容器适配器之priority_queue
template <class T, class Container = vector<T>, class Compare = less<type ...
- [C/C++标准库]_[0基础]_[优先队列priority_queue的使用]
std::priority_queue 场景: 1. 对于一个任务队列,任务的优先级由任务的priority属性指明,这时候就须要优先级越高的先运行.而queue并没有排序功能,这时priority_ ...
- 【STL】c++ priority_queue的使用方法
最开始在项目文档看到priority_queue这个模板时,还以为是自己定义的呢,后来查了一下,原来这是STL中存在的一种优先队列. 1.最简单的使用方法 std::priority_queue< ...
- 浅谈C++ STL中的优先队列(priority_queue)
从我以前的博文能看出来,我是一个队列爱好者,很多并不是一定需要用队列实现的算法我也会采用队列实现,主要是由于队列和人的直觉思维的一致性导致的. 今天讲一讲优先队列(priority_queue),实际 ...
- 让priority_queue支持小根堆的几种方法
点击这里了解什么是priority_queue 前言 priority_queue默认是大根堆,也就是大的元素会放在前面 例如 #include<iostream> #include< ...
- C++标准模板库(STL)之Priority_Queue
1.Priority_Queue的常用用法 priority_queue:优先队列,底层是使用堆来实现的.优先队列中,队首元素一定是当前队列中优先级最高的哪一个. a (优先级3),b(优先级4),c ...
随机推荐
- ImageView缩放选项
ImageView.ScaleType 将图片边界缩放到所在view边界时的缩放选项. Options for scaling the bounds of an image to the bounds ...
- 在.Net中实现自己的简易AOP
RealProxy基本代理类 RealProxy类提供代理的基本功能.这个类中有一个GetTransparentProxy方法,此方法返回当前代理实例的透明代理.这是我们AOP实现的主要依赖. 新建一 ...
- TinyWeb v1.0 正式完成第一个Release版本(功能基于 libuv 跨平台库)
使用方法很简单,很容易融入现有项目,使现有项目拥有Web网站功能和WebSocket,以及Socket直连! 并且包含了一个跨平台(windows/linux)工具集合; 嗯,也挺棒的^,^ 在项目中 ...
- 【翻译】MongoDB指南/CRUD操作(二)
[原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(二) 主要内容: 更新文档,删除文档,批量写操作,SQL与MongoDB映射图,读隔离(读关 ...
- [转]利用URLConnection来发送POST和GET请求
URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和 URL 之间的通信链接.程序可以通过URLConnection实例向该URL发送请求.读取U ...
- [C#] 走进 LINQ 的世界
走进 LINQ 的世界 序 在此之前曾发表过三篇关于 LINQ 的随笔: 进阶:<LINQ 标准查询操作概述>(强烈推荐) 技巧:<Linq To Objects - 如何操作字符串 ...
- 如何将VCSA添加到微软域控环境,并且实现微软域账号登陆vCenter
v:* { } o:* { } w:* { } .shape { } p.msonormal,li.msonormal,div.msonormal { margin: 0cm; margin-bott ...
- Twproject Gantt开源甘特图功能扩展
1.Twproject Gantt甘特图介绍 Twproject Gantt 是一款基于 jQuery 开发的甘特图组件,也可以创建其它图表,例如任务树(Task Trees).内置编辑.缩放和 CS ...
- 在redis中使用lua脚本让你的灵活性提高5个逼格
在redis的官网上洋洋洒洒的大概提供了200多个命令,貌似看起来很多,但是这些都是别人预先给你定义好的,但你却不能按照自己的意图进行定制, 所以是不是感觉自己还是有一种被束缚的感觉,有这个感觉就对了 ...
- How to accept Track changes in Microsoft Word 2010?
"Track changes" is wonderful and remarkable tool of Microsoft Word 2010. The feature allow ...