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 ...
随机推荐
- 【sql】关联查询+表自关联查询
表: 经销商 dealer 字段 uid parent_uid name 联系人 contact 字段 uid dealer_id contact_main 需求: 想要查询到经销商的信 ...
- 基于3D Vision眼镜的OSG立体显示 【转】
http://blog.csdn.net/qq_20038925/article/details/50510565 OSG 立体显示 3D Vision眼镜:所实现的是被动立体. 1.本人最近在做os ...
- OOP几大原则【转】
设计模式遵循的一般原则: 1.开-闭原则(Open-Closed Principle, OCP):一个软件实体应当对扩展开发,对修改关闭.说的是,再设计一个模块的时候,应当使这个模块可以在不被修改的前 ...
- BIO的简单Demo
package jesse.test1; import java.io.BufferedReader; import java.io.IOException; import java.io.Input ...
- Mounting the NFS share on a Windows server
今天遇到一个相当奇怪的问题,在windows 上mount LINUX NFS, powershell 脚本可以成功, 用图形界面也可以成功,但BATCH就是不行.提示53网络错误. 不过公司已经有人 ...
- 报错:numRecords must not be negative
报错的原因:删除已经使用过的kafka topic,然后新建同名topic 解决方法:把topic名字换一下 (有其他更好的解决方法,可以不修改topic名)
- centos 7 安装gcc g++
yum install gcc gcc-c++ over. ps:如果系统报yum命令未找到,退出重新登陆试试root.
- Win7如何开启管理员账户
打开运行对话框,在LUSRMGR.MSC里,左边点用户,在右边栏里右击Administrator选择属性,去掉账户已禁用这个选项前面的勾.我也问过这个问题,确实如版主说的这样可解决这个问题,但有个问题 ...
- LoadRunner测试AJAX
什么是AJAX? Ajax, shorthand for Asynchronous JavaScript and XML, is a web development technique for cre ...
- 使用 原生js 制作插件 (javaScript音乐播放器)
1.引用页面 index.html <!DOCTYPE html> <html lang="en"> <head> <meta chars ...