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 ...
随机推荐
- Hibernatel框架关联映射
Hibernatel框架关联映射 Hibernate程序执行流程: 1.集合映射 需求:网络购物时,用户购买商品,填写地址 每个用户会有不确定的地址数目,或者只有一个或者有很多.这个时候不能把每条地址 ...
- SQL Server技术内幕笔记合集
SQL Server技术内幕笔记合集 发这一篇文章主要是方便大家找到我的笔记入口,方便大家o(∩_∩)o Microsoft SQL Server 6.5 技术内幕 笔记http://www.cnbl ...
- ASP.NET Aries 入门开发教程2:配置出一个简单的列表页面
前言: 朋友们都期待我稳定地工作,但创业公司若要躺下,也非意念可控. 若人生注定了风雨飘摇,那就雨中前行了. 最机开始看聊新的工作机会,欢迎推荐,创业公司也可! 同时,趁着自由时间,抓紧把这系列教程给 ...
- 使用 Roslyn 编译器服务
.NET Core和 .NET 4.6中 的C# 6/7 中的编译器Roslyn 一个重要的特性就是"Compiler as a Service",简单的讲,就是就是将编译器开放为 ...
- Unity 序列化
Script Serialization http://docs.unity3d.com/Manual/script-Serialization.html 自定义序列化及例子: http://docs ...
- LeetCode[5] 最长的回文子串
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- [C#][算法] 用菜鸟的思维学习算法 -- 马桶排序、冒泡排序和快速排序
用菜鸟的思维学习算法 -- 马桶排序.冒泡排序和快速排序 [博主]反骨仔 [来源]http://www.cnblogs.com/liqingwen/p/4994261.html 目录 马桶排序(令人 ...
- android studio 使用 jni 编译 opencv 完整实例 之 图像边缘检测!从此在andrid中自由使用 图像匹配、识别、检测
目录: 1,过程感慨: 2,运行环境: 3,准备工作: 4,编译 .so 5,遇到的关键问题及其解决方法 6,实现效果截图. (原创:转载声明出处:http://www.cnblogs.com/lin ...
- nodejs利用ajax实现网页无刷新上传图片
nodejs利用ajax实现网页无刷新上传图片 标签(空格分隔): nodejs 通常情况下上传图片是要通过提交form表单来实现的,但是这又不可避免的产生了网页转. 利用ajax技术和FormDat ...
- 显示本地openssl支持的加密算法
参考页面: http://www.yuanjiaocheng.net/webapi/parameter-binding.html http://www.yuanjiaocheng.net/webapi ...