最大堆(max-heap)和最小堆(min-heap)
来源:互联网 发布:淘宝跟客服聊天 编辑:程序博客网 时间:2024/05/16 18:32
在STL中,二叉堆是通过priority_queue的类模板实现的,标准头文件是<queue>。在STL中实现的是最大堆(max-heap)。
成员函数有:
void push (const Object & x);const Object & top() const;void pop();bool empty();void clear();
优先队列模版用如下参数初始化:项类型,容器类型(几乎总是使用存储项的vector)和比较器。最后两个参数是默认值,并且默认情况下得到最大堆。使用greater函数对象作为比较器可以得到最小堆。
示例:
#include <iostream>#include <vector>#include <queue>#include <functional>#include <string>using namespace std;//Empty the priority queue and print its contents.template <typename PriorityQueue>void dumpContents ( const string & msg, PriorityQueue & pq){ cout << msg << ":" << endl; while (!pq.empty() ) { cout << pq.top() << endl; pq.pop(); }}//Do some inserts and removes (done in dumpContents)int main (){ priority_queue<int> maxPQ; priority_queue<int, vector<int>, greater<int> > minPQ; min.PQ.push(4); minPQ.push(3); minPQ.push(5); max.PQ.push(4); maxPQ.push(3); maxPQ.push(5); dumpConstents ( "minPQ", minPQ ); // 3 4 5 dumpConstents ( "maxPQ", maxPQ ); // 5 4 3 return 0;}
- 最大堆(max-heap)和最小堆(min-heap)
- Java实现用最大堆和最小堆查找中位数 Find median with min heap and max heap in Java
- 最小堆和最大堆 heap (zz)
- 最大堆排序输出(max heap sort)
- 双端堆/最小-最大堆/Double-Ended Heap ?
- heap最大(小)堆
- STL源码—heap最大堆,最小堆
- STL源码—heap最大堆,最小堆(TT)
- min heap priority queue 最小堆实现的优先队列
- heap 堆
- heap 堆
- 堆(heap)
- 堆(heap)
- Heap 堆
- 堆 (heap)
- 堆heap
- 堆---heap
- 堆 Heap
- android imageview 多点触碰(MultiTouch)实现图片拖拽移动缩放
- Java回调方法
- android各种组件的监听器
- ExtJS快速入门指南
- 函数 - 返回值
- 最大堆(max-heap)和最小堆(min-heap)
- startup命令显示的结果可以看出数据库启动的三个阶段
- IO 缓存<-----字节<-----字符
- 润乾平台 - 用已有的xml与json文件为数据集设置宏和参数
- Java 代码功能片段(待续。。。)
- iOS7开发技巧
- php-魔术方法
- form内只有一个输入框时,按回车不让自动提交的解决方案
- 宗教信仰(并查集)