stl中的heap使用
来源:互联网 发布:linux beam.smp 编辑:程序博客网 时间:2024/05/21 09:42
stl中的堆默认是最大堆,要想用最小堆的话,必须要在push_heap,pop_heap,make_heap等每一个函数后面加第三个参数greater<int>(),括号不能省略。
1、make_heap:使序列变成堆
原型:template <class RandomAccessIterator>
void make_heap ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void make_heap ( RandomAccessIterator first, RandomAccessIterator last,
Compare comp );
范例:
// range heap example#include <iostream>#include <algorithm>#include <vector>using namespace std;int main () { int myints[] = {10,20,30,5,15}; vector<int> v(myints,myints+5); make_heap (v.begin(),v.end()); cout << "initial max heap : " << v.front() << endl; pop_heap (v.begin(),v.end()); v.pop_back(); cout << "max heap after pop : " << v.front() << endl; v.push_back(99); push_heap (v.begin(),v.end()); cout << "max heap after push: " << v.front() << endl; sort_heap (v.begin(),v.end()); cout << "final sorted range :"; for (unsigned i=0; i<v.size(); i++) cout << " " << v[i]; cout << endl; return 0;}2、push_heap:压栈(入栈)
原型:
template <class RandomAccessIterator>
void push_heap ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void push_heap ( RandomAccessIterator first, RandomAccessIterator last,
Compare comp );
3、pop_heap:弹栈(出栈)
原型:template <class RandomAccessIterator>
void pop_heap ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void pop_heap ( RandomAccessIterator first, RandomAccessIterator last,
Compare comp );
范例:
// range heap example#include <iostream>#include <algorithm>#include <vector>using namespace std;int main () { int myints[] = {10,20,30,5,15}; vector<int> v(myints,myints+5); vector<int>::iterator it; make_heap (v.begin(),v.end()); cout << "initial max heap : " << v.front() << endl; pop_heap (v.begin(),v.end()); v.pop_back(); cout << "max heap after pop : " << v.front() << endl; v.push_back(99); push_heap (v.begin(),v.end()); cout << "max heap after push: " << v.front() << endl; sort_heap (v.begin(),v.end()); cout << "final sorted range :"; for (unsigned i=0; i<v.size(); i++) cout << " " << v[i]; cout << endl; return 0;}
4、sort_heap:对堆排序原型:template <class RandomAccessIterator>
void sort_heap ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void sort_heap ( RandomAccessIterator first, RandomAccessIterator last,
Compare comp );
范例:
// range heap example#include <iostream>#include <algorithm>#include <vector>using namespace std;int main () { int myints[] = {10,20,30,5,15}; vector<int> v(myints,myints+5); vector<int>::iterator it; make_heap (v.begin(),v.end()); cout << "initial max heap : " << v.front() << endl; pop_heap (v.begin(),v.end()); v.pop_back(); cout << "max heap after pop : " << v.front() << endl; v.push_back(99); push_heap (v.begin(),v.end()); cout << "max heap after push: " << v.front() << endl; sort_heap (v.begin(),v.end()); cout << "final sorted range :"; for (unsigned i=0; i<v.size(); i++) cout << " " << v[i]; cout << endl; return 0;}
注:例子来源于www.cplusplus.com网站
- stl中的heap使用
- STL中的heap操作
- STL中的heap算法
- 使用stl实现heap
- 关于 STL 中的 heap ( 堆 )
- STL 中的 heap 与 priority_queue
- STL的heap的使用
- STL heap底层及使用
- c++中STL之heap, priority_queue使用
- c++中STL之heap, priority_queue使用
- stl的heap和priority_queue的使用
- heap stl
- STL【Heap】
- stl-heap
- heap STL
- STL Heap
- STL heap
- C++11新特性应用--介绍几个新增的便利算法(stl中的heap使用,最大堆)
- ZOJ 1109 Language of FatMouse
- 《学习OpenCV》练习题第四章第三题a
- webkit真正可以编译的过程
- 给ActiveX控件授权,加数字签名
- 开源项目之Android RibbonMenu(导航菜单)
- stl中的heap使用
- Joomla 3.x_component如何打包安装和卸载
- 我的敏捷思想成长之旅
- 十天学会DIV+CSS第十天 div+css网页标准布局实例教程
- 《学习OpenCV》练习题第四章第三题b
- SQL 存储过程
- 【华安php入门系列】--第1天-php的执行方式、执行过程
- ubuntu11.10 samba服务器配置
- 略读六部计算机名著