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 ...
随机推荐
- 安网讯通签约孟强美容CRM
整形美容CRM软件是辽宁安网讯通有限公司为孟强整形医院定制开发的一套客户关系管理软件,软件专门针对整形美容行业专科门诊的需求,能满足大中小整形美容机构或或各种专科科室的日常业务需求. 主要功能包括: ...
- 网络采集软件核心技术剖析系列(6)---将任意博主的全部博文下载到SQLite数据库中并通过Webbrower显示(将之前的内容综合到一起)
一 本系列随笔目录及本节代码下载 自己开发的豆约翰博客备份专家软件工具问世3年多以来,深受广大博客写作和阅读爱好者的喜爱.同时也不乏一些技术爱好者咨询我,这个软件里面各种实用的功能是如何实现的. 该软 ...
- 【Node.js】2.开发Node.js选择哪个IDE 开发工具呢
安装完Node.js之后,就要为它选择一个有利的IDE用于开发. 相比较了多个IDE之后,定位在webstrom和sublime上. 有一个简单的比较: webstorm功能很丰富,前端开发工具的集大 ...
- 小二助手(react应用框架)-http访问
浏览地址http://118.25.217.253:4000 账号test密码123 qq讨论群:836719189 要写这个系统,就需要数据来源,让我们先来看看如果通过客户端调用服务端api拿到 ...
- 基础普及-Jar、War、Ear
名词解释 Jar文件(扩展名为. Jar) 包括Java类的普通库.资源(resources).辅助文件 (auxiliary files)等 War文件(扩展名为.War) 包括所有Web应用程序. ...
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-人机界面如何自动运行,不让用户干涉,设置起始界面
右击视图管理器,添加一个TargetVisualization 在起始视图中点击右边的按钮,然后选择一个HMI作为起始HMI 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: h ...
- UIScreenAdaptive
using UnityEngine; namespace Com.Xyz.UI { [ExecuteInEditMode] [RequireComponent(typeof(UIRoot))] pub ...
- Vue 进入/离开动画
1.示例代码 (注:写到vue单文件中了) <template> <div> <button v-on:click="show = !show"> ...
- Android 之UI自适应解决方式
1.概况 作为Android开发者,最头疼的莫过于让自己开发的程序在不同终端上面的显示效果看起来尽量一致(当然.假设要充分利用大屏幕的优势另当别论).在全球范围内来讲.android有着数以亿计的设备 ...
- windows上使用的免费连接linux终端xshell6,xftp6下载
xshell 6 https://www.netsarang.com/download/down_form.html?code=622 xftp 6 https://www.netsarang.com ...