STL中并没有把heap作为一种容器组件,heap的实现亦需要更低一层的容器组件(诸如list,array,vector)作为其底层机制。Heap是一个类属算法,包含在algorithm头文件中。虽然STL中关于heap默认调整成的是大顶堆,但却可以让用户利用自定义的compare_fuction函数实现大顶堆或小顶堆。heap的低层机制vector本身就是一个类模板,heap基于vector便实现了对各种数据类型(无论基本数据类型还是用户自定义的数据类型)的堆排(前提是用户自定义的数据类型要提供比较机制compare_fuction函数)。
 
STL里面的堆操作一般用到的只有4个。
make_heap();pop_heap();push_heap();sort_heap();
 
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 push_heap(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[]={,,,,,,,,,,,};
make_heap(&number[],&number[]);
//结果是:51 35 40 23 29 20 26 22 19 12 17 15
for(i=;i<;i++)
printf("%d ",number[i]);
printf("\n");
make_heap(&number[],&number[],cmp);
//结果:12 17 15 19 23 20 26 51 22 29 35 40
for(i=;i<;i++)
printf("%d ",number[i]);
printf("\n"); //加入元素8
number[]=;
//加入后调整
push_heap(&number[],&number[],cmp);
//结果:8 17 12 19 23 15 26 51 22 35 40 20
for(i=;i<;i++)
printf("%d ",number[i]);
printf("\n"); //弹出元素8
pop_heap(&number[],&number[],cmp);
//结果:12 17 15 19 23 20 26 51 22 29 35 40
for(i=;i<;i++)
printf("%d ",number[i]);
printf("\n"); sort_heap(&number[],&number[],cmp);
//结果不用说都知道是有序的了!
for(i=;i<;i++)
printf("%d ",number[i]);
return ;
}
 

STL之heap使用简介的更多相关文章

  1. L2-012. 关于堆的判断(STL中heap)

    L2-012. 关于堆的判断   将一系列给定数字顺序插入一个初始为空的小顶堆H[].随后判断一系列相关命题是否为真.命题分下列几种: “x is the root”:x是根结点: “x and y ...

  2. STL之heap与优先级队列Priority Queue详解

    一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的m ...

  3. STL中heap相关函数

    heap并不是属于STL中的containers,而是在<algorithm>下提供了相关的函数 make_heap,sort_heap,pop_heap,push_heap 函数的说明: ...

  4. STL之priority_queue使用简介

    优先队列容器也是一种从一端入队,另一端出对的队列.不同于一般队列的是,队列中最大的元素总是位于队首位置,因此,元素的出对并非按照先进先出的要求,将最先入队的元素出对,而是将当前队列中的最大元素出对. ...

  5. SGI STL泛型heap算法分析

    heap性质 heap本质是用一个数组表示的完全二叉树,并且父节点总是大于(或者小于)子节点的值.在STL中用于实现优先队列(priority_queque).堆排序是排序算法中是稳定效率最高的一种. ...

  6. STL中heap算法(堆算法)

     ①push_heap算法 以下是push_heap算法的实现细节.该函数接收两个迭代器,用来表现一个heap底部容器(vector)的头尾,而且新元素已经插入究竟部的最尾端. template ...

  7. STL之heap

    STL的堆操作 STL里面的堆操作一般用到的只有4个:make_heap();.pop_heap();.push_heap();.sort_heap(); 他们的头文件函数是#include < ...

  8. STL之heap学习

    C++标准库中的堆-heap make_heap函数,包括两个参数(begin(number),end(number)).(左闭右开) pop_heap函数,包括两个参数,起始位置和终止位置,将当前区 ...

  9. STL之容器(containers) 简介

    什么是容器? 容器用来存储数据的,数据可以是用户自定义类型(对象),也可以是预定义类型,c++中的容器主要使用如vector,list (顺序容器) 这些都是已经封装好了. 1.结构(struct): ...

随机推荐

  1. stixel提升思路总结

    1.用psmnet获得更好的disparity 2.用edgebox获得整个rgb图片的边缘,然后通过原本的stixel的上下边缘去寻找最优,用两个的边缘去重新得到一个新的边缘,但获得的轮廓不仅仅是外 ...

  2. vim实用配置

    "编码set encoding=utf-8"显示行号set number"语法高亮度显示syntax on "自动缩进set autoindent"t ...

  3. ADO.net中常用的对象有哪些?

    ADO.net中常用的对象有哪些?分别描述一下. 答:Connection 数据库连接对像 Command 数据库命令 DataReader 数据读取器 DataSet 数据集 DataReader与 ...

  4. SpringBoot学习13:springboot异常处理方式3(使用@ControllerAdvice+@ExceptionHandle注解)

    问题:使用@ExceptionHandle注解需要在每一个controller代码里面都添加异常处理,会咋成代码冗余 解决方法:新建一个全局异常处理类,添加@ControllerAdvice注解即可 ...

  5. java基础必备单词讲解 day six

    development development development development 开发 development developmentenvironment environment en ...

  6. architecture x86_64(Error)

    undefined symbols for architecture x86_64 错误如下 因为提示文件非第三方文件,最初尝试使用以下方式处理 iOS :undefined symbols for ...

  7. Json数据常用操作

    JSON字符串: var str1 = '{ "name": "cs", "sex": "man" }'; JSON对象 ...

  8. Springcloud Eureka 启动失败:ERROR org.springframework.boot.SpringApplication - Application run failed

    在测试Euruka作为服务注册中心的时候碰到了这个问题 [main] ERROR org.springframework.boot.SpringApplication - Application ru ...

  9. sql的使用

    1.自动获取最新订单号 select concat('XJDD',DATE_FORMAT(now(),'%Y%m%d'), LPAD(( FOR )) , max(SUBSTRING(inquiryn ...

  10. 服务器缺少vcruntime140.dll,无法运行

    Redis用了一段时间,有的时候,调试的时候,RedisDesktop是个不错的工具 当我想在服务器上安装的时候,才发现服务器64位的环境里面运行出错了 百度上有共享dll出来的,但是基本都没法用,虽 ...