C++ heap
来源:互联网 发布:仿168开奖网源码 编辑:程序博客网 时间:2024/05/24 07:37
make_heap();、pop_heap();、push_heap();、sort_heap();
他们的头函数是algorithm
首先是make_heap();
他的函数原型是:
void make_heap(first_pointer,end_pointer,compare_function);
一个参数是数组或向量的头指针,第二个向量是尾指针。第三个参数是比较函数的名字
。在缺省的时候,默认是大跟堆。(下面的参数都一样就不解释了)
作用:把这一段的数组或向量做成一个堆的结构。范围是(first,last)
然后是pop_heap();
它的函数原型是:
void pop_heap(first_pointer,end_pointer,compare_function);
作用:pop_heap()不是真的把最大(最小)的元素从堆中弹出来。而是重新排序堆。它
把first和last交换,然后将[first,last-1)的数据再做成一个堆。
接着是push_heap()
void pushheap(first_pointer,end_pointer,compare_function);
作用:push_heap()假设由[first,last-1)是一个有效的堆,然后,再把堆中的新元素加
进来,做成一个堆。
最后是sort_heap()
void sort_heap(first_pointer,end_pointer,compare_function);
作用是sort_heap对[first,last)中的序列进行排序。它假设这个序列是有效堆。(当然
,经过排序之后就不是一个有效堆了)
下面是例程:
#include<algorithm>
#include<cstdio>
using namespace std;
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int i,number[20]={29,23,20,22,17,15,26,51,19,12,35,40};
make_heap(&number[0],&number[12]);
//结果是:51 35 40 23 29 20 26 22 19 12 17 15
for(i=0;i<12;i++)
printf("%d ",number[i]);
printf("\n");
make_heap(&number[0],&number[12],cmp);
//结果:12 17 15 19 23 20 26 51 22 29 35 40
for(i=0;i<12;i++)
printf("%d ",number[i]);
printf("\n");
//加入元素8
number[12]=8;
//加入后调整
push_heap(&number[0],&number[13],cmp);
//结果:8 17 12 19 23 15 26 51 22 35 40 20
for(i=0;i<13;i++)
printf("%d ",number[i]);
printf("\n");
//弹出元素8
pop_heap(&number[0],&number[13],cmp);
//结果:12 17 15 19 23 20 26 51 22 29 35 40
for(i=0;i<13;i++)
printf("%d ",number[i]);
printf("\n");
sort_heap(&number[0],&number[12],cmp);
//结果不用说都知道是有序的了!
for(i=0;i<12;i++)
printf("%d ",number[i]);
return 0;
}
网上资料:
http://wenku.baidu.com/view/6adf1773f242336c1eb95ef2.html
http://www.docin.com/p-159439083.html
http://www.cnblogs.com/rocketfan/archive/2009/11/28/1612665.html
- Stack & Heap in C/C++
- Stack & Heap in C/C++ .
- Stack & Heap in C/C++
- C/C++ STACK &HEAP 区别
- C/C++ STACK &HEAP 区别
- C/C++ STACK &HEAP 区别
- C/C++ STACK &HEAP 区别
- C/C++ STACK &HEAP 区别
- C/C++ STACK &HEAP 区别
- C实现heap堆排序
- CodeForces 681C Heap Operations
- codeforces 681C Heap Operations
- CodeForces 681C Heap Operations
- CodeForces 681C Heap Operations
- Heap
- Heap
- heap
- Heap
- Android SMSC设置失败短信发送失败问题
- 最简单的json联级下拉框(我自己写的)
- Topcoder.SRM527.Div1.T2
- linux基础知识
- 谈谈近期的安全事件
- C++ heap
- JExcel入门学习总结 .
- 打造一个基于OSGi的Web Application——在WebApplication中启动OSGi
- 互联网之父等83位专家联名炮轰美国互联网政策
- 华为光纤猫HG8240R设置
- 实战Memcached缓存系统(3)Memcached配置参数初解
- awk删除文件最后一列
- android控件13 AutoCompleteTextView
- 15.3.3 将函数转换成“行为函数”