用STL实现堆容器
来源:互联网 发布:网络清洁器 编辑:程序博客网 时间:2024/05/17 10:55
这容器内部使用vector,外部只提供empty,pop,push,三个操作。其中pop操作剔除顶,并返回顶部元素
#include <iostream>#include <vector>#include <algorithm>using namespace std;template<typename T>class heap{public:bool empty(){ return data_.empty();}T pop(void );void push(const T& t);private:vector<T> data_;};template<typename T>void heap<T>::push(const T& t){data_.push_back(t);make_heap(data_.begin(),data_.end());}template<typename T>T heap<T>::pop(void){T t=data_.front();data_.erase(data_.begin());make_heap(data_.begin(),data_.end());return t;}int main(){heap<int> h;for (int i=1;i<11;i++){h.push(i);}while (!h.empty()){cout<<h.pop()<<endl;}}
10
9
8
7
6
5
4
3
2
1
请按任意键继续. . .
0 0
- 用STL实现堆容器
- 用C++实现STL容器vector
- 用C++实现STL容器list
- 用C++实现STL容器stack
- 用STL容器实现Dijkstraalg算法
- STL vector 容器实现
- STL容器的实现原理
- STL容器的实现原理
- STL容器的实现原理
- STL容器实现方式归纳
- STL容器的实现原理
- STL容器之底层实现
- STL容器的实现原理
- STL容器的实现原理
- stl容器list部分实现
- STL vector容器自己实现
- c++ STL 容器底层实现
- STL容器的底层实现
- 使用 yii 实现 REST 风格接口
- Erlang实现的简易HTTP服务器
- Android listView
- spring hibernate c3p0 mysql 配置
- 用户为什么离开你?
- 用STL实现堆容器
- 哈希表入门题目总结(HDU 1280、1425、2027、3833、1496、2648 POJ 1200)
- XDU Problem 1255 - 数列游戏
- Git 常用指令详解
- Hibernate的generator属性
- 编程中犯的错误
- Hype Cycle (技术成熟度曲线)
- 一些算法的MapReduce实现——矩阵分块乘法计算(2)
- Debian 7下配置samba和Windows8共享