STL学习(六)heap学习
来源:互联网 发布:淘宝服务市场推广 编辑:程序博客网 时间:2024/06/05 04:51
建立堆make_heap(),在堆中添加数据push_heap(),在堆中删除数据pop_heap()和堆排序sort_heap():
头文件 #include <algorithm>
void make_heap(first_pointer,end_pointer,compare_function)
void pop_heap(first_pointer,end_pointer,compare_function)
void push_heap(first_pointer,end_pointer,compare_function)
void sort_heap(first_pointer,end_pointer,compare_function)
函数说明:
make_heap
把一段的数组或向量做成一个堆的结构。范围是(first,last)pop_heappop_heap()不是真的把最大(最小)的元素从堆中弹出来。
而是重新排序堆。它把first和last交换,然后将[first,last-1)
的数据再做成一个堆。push_heappush_heap()假设由[first,last-1)是一个有效的堆,然后,再
把堆中的新元素加进来,做成一个堆。sort_heapsort_heap对[first,last)中的序列进行排序。它假设这个序列
是有效堆。(当然,经过排序之后就不是一个有效堆了)
#include <iostream>#include <vector>#include <algorithm>using namespace std;void printVector(vector<int> vec){ vector<int>::iterator it = vec.begin(); for(;it!=vec.end();it++) cout<<*it<<" "; cout<<endl; cout<<endl;}int main(){ int num[10] = {101,22,13,96,54,23,204,11,5,14}; vector<int> v1(num,num+10); printVector(v1); make_heap(v1.begin(),v1.end()); printVector(v1); /*first: push_back() second:push_heap()*/ v1.push_back(1000); push_heap(v1.begin(),v1.end()); printVector(v1); /*first: pop_heap second: pop_back*/ pop_heap(v1.begin(),v1.end()); v1.pop_back(); printVector(v1); sort_heap(v1.begin(),v1.end()); printVector(v1); return 0;}
0 0
- STL学习(六)heap学习
- STL中heap学习
- C++ STL源码学习(priority_queue内部heap篇)
- STL学习记录(六):List
- SGI-STL学习笔记之heap算法
- SGI STL 学习笔记三 heap
- STL学习之heap & priority queue
- stl源码剖析 详细学习笔记heap
- C++ STL学习——heap
- 例说数据结构&STL(六)——heap
- 【STL学习】堆相关算法详解与C++编程实现(Heap)
- 转【STL学习】堆相关算法详解与C++编程实现(Heap)
- STL学习(六)--map/multimap用法详解
- STL学习之六:queue用法示例
- 【C++ STL学习之六】STL算法之for_each
- 【C++ STL学习之六】STL算法之for_each
- 【C++ STL学习之六】STL算法之for_each
- C++ STL学习之六:配接器queue深入学习
- C语言 16进制字符串 转 16进制 方法
- Valid Parentheses
- Android编码规范
- C语言 内存操作函数
- UVALive 6669 Hidden Tree DP
- STL学习(六)heap学习
- 移植64位发生的问题(跟cltq指令有关)
- DSP/BIOS硬件中断管理
- jQuery 2.0发布,不支持IE 6/7/8
- test
- Trim问题
- centos6.4 裸机装环境
- spring技术内幕13-SqlMapClientTemplate对Ibatis的封装
- CSDN BLOG is not good for saving codes..