priority_queue优先队列/C++
priority_queue优先队列/C++
概述
priority_queue是一个拥有权值观念的queue,只允许在底端加入元素,并从顶端取出元素。
priority_queue带有权值观念,权值最高者,排在最前面。
缺省情况下priority_queue系利用一个max-heap完成,后者是一个以vector表现的complete binary tree。

定义
由于priority_queue完全以底部容器为根据,再加上heap处理规则,所以其实现非常简单。缺省情况下是以vector为底部容器。
priority_queue的所有元素,进出都有一定的规则,只有queue顶端的元素(权值最高者),才有机会被外界取用。priority_queue不提供遍历功能,也不提供迭代器。
底部用到了:make_heap,push_heap,pop_heap(三个都是泛型算法)
push_heap: 先利用底层容器的push_back()将新元素推入末尾,再重排heap。
pop_heap: 从heap内取出一个元素。它并不是真正将元素弹出,而是重排heap,然后再以底层容器的pop_back()取得被弹出的元素。
template<
class T,
class Container = std::vector<T>,
class Compare = std::less<typename Container::value_type>
> class priority_queue;
- T - The type of the stored elements. The behavior is undefined if T is not the same type as Container::value_type. (since C++17)
- Container - The type of the underlying container to use to store the elements. The container must satisfy the requirements of SequenceContainer, and its iterators must satisfy the requirements of RandomAccessIterator. Additionally, it must provide the following functions with the usual semantics:
front()
push_back()
pop_back()
The standard containers std::vector and std::deque satisfy these requirements.
//底层只能是vector 和 deque 实现- Compare - A Compare type providing a strict weak ordering.
std::greater可以使最小元素出现在队首(内部是最小堆)
操作
| 常用函数 | 作用 |
|---|---|
| top | 取队头元素 |
| empty | 判断优先队列是否为空 |
| size | 优先队列中元素个数 |
| push | 向优先队列中添加一个元素 |
| pop | 弹出队头元素(返回值是void) |
example
#include <functional>
#include <queue>
#include <vector>
#include <iostream>
template<typename T> void print_queue(T& q) {
while(!q.empty()) {
std::cout << q.top() << " ";
q.pop();
}
std::cout << '\n';
}
int main() {
std::priority_queue<int> q;
for(int n : {1,8,5,6,3,4,0,9,7,2})
q.push(n);
print_queue(q);
std::priority_queue<int, std::vector<int>, std::greater<int> > q2;
for(int n : {1,8,5,6,3,4,0,9,7,2})
q2.push(n);
print_queue(q2);
// Using lambda to compare elements.
auto cmp = [](int left, int right) { return (left ^ 1) < (right ^ 1);};
std::priority_queue<int, std::vector<int>, decltype(cmp)> q3(cmp);
for(int n : {1,8,5,6,3,4,0,9,7,2})
q3.push(n);
print_queue(q3);
}
output:
9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9
8 9 6 7 4 5 2 3 0 1
http://www.frankyang.cn/2017/08/31/priorityqueue/
priority_queue优先队列/C++的更多相关文章
- 第20章 priority_queue优先队列容器
/* 第20章 priority_queue优先队列容器 20.1 priority_queue技术原理 20.2 priority_queue应用基础 20.3 本章小结 */ // 第20章 pr ...
- stack堆栈容器、queue队列容器和priority_queue优先队列容器(常用的方法对比与总结)
stack堆栈是一个后进先出的线性表,插入和删除元素都在表的一端进行. stack堆栈的使用方法: 采用push()方法将元素入栈: 采用pop()方法将元素出栈: 采用top()方法访问栈顶元素: ...
- (转)【C++ STL】细数C++ STL 的那些事 -- priority_queue(优先队列)
装载自http://blog.csdn.net/tianshuai1111/article/details/7652553 一,概述 priority_queue是拥有权值观念的queue,它允许加入 ...
- priority_queue 优先队列
优先队列是单向队列的一种,可以按照默认或自定义的一种方式来对队列中的数据进行动态排序 template<class _Ty, class _Container = vector<_Ty&g ...
- 【转】priority_queue优先队列
转自:http://www.cppblog.com/shyli/archive/2007/04/06/21366.html http://www.cppblog.com/shyli/archive/2 ...
- priority_queue(优先队列):排序不去重
C++优先队列类似队列,但是在这个数据结构中的元素按照一定的断言排列有序. 头文件:#include<queue> 参数:priority_queue<Type, Container ...
- STL - priority_queue(优先队列)
参考:http://www.cnblogs.com/xzxl/p/7266404.html 一.基本定义: 优先队列容器与队列一样,只能从队尾插入元素,从队首删除元素.但是它有一个特性,就是队列中最大 ...
- STL之priority_queue(优先队列)
priority_queue是一个容器适配器,在这个容器里第一个数据元素是最大的.它的使用场景是什么样:如果12306抢票,为什么黄牛能抢这么多票,感觉12306那边的请求队列是一个优先队列,黄牛的请 ...
- 标准模板库(STL)学习指南之priority_queue优先队列
转载自CSDN博客:http://blog.csdn.net/suwei19870312/article/details/5294016 priority_queue 调用 STL里面的 make_h ...
随机推荐
- debian 6软件更新源列表
deb http://ftp.debian.org/debian/ squeeze main non-free contribdeb http://ftp.debian.org/debian/ squ ...
- [SpringMVC+redis]自定义aop注解实现控制器访问次数限制
原文:http://www.cnblogs.com/xiaoyangjia/p/3762150.html?utm_source=tuicool 我们需要根据IP去限制用户单位时间的访问次数,防止刷手机 ...
- T-sql脚本规范
一.创建表 IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'表名') AND type in (N'U') ...
- centos6.9安装xampp后报错:egrep: error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory
1.centos6.9安装xampp(xampp-linux-x64-7.0.21-0-installer.run)后启动的时候,报错: egrep: error while loading shar ...
- solr 统计频率(term frequency)
1.统计单词在某个字段出现的频率次数 term frequency实现使用了function query. 例如统计‘公司’这个关键字在text这个字段中出现的次数 在返回的时候进行计算统计,即在返回 ...
- Invalidate、RedrawWindow与UpdateWindow的差别
一:什么时候才会发生重绘窗体的消息? 当须要更新或又一次绘制窗体的外观时,应用程序就会发送WM_PAINT消息. 对窗体进行又一次绘制. 二:Invalidate() -- RedrawW ...
- 转: 私人珍藏的Chrome插件,吐血推荐
转:来自 http://stormzhang.com/devtools/2016/01/15/google-chrome-extension/
- spring常用的一些注解以及注解注入总结
常用的spring注解有如下几种: @Controller@Service@Autowired@RequestMapping@RequestParam@ModelAttribute@Cacheable ...
- jsp 页面图片为圆形
直接设置img标签的style属性即可 <img alt="" src="链接地址" style="width: 80px;height: 80 ...
- 虚拟机oracle virtualbox 上安装centos6.5 网络设置
上篇文章写到,在虚拟机上安装centos6.5,结果依照文章非常顺利的安装了,可是用yum安装软件的时候.报错,源有问题,不能下载,然后ping一下摆渡.非常悲催 dns解析不了,cat /etc/r ...