STL的堆操作

STL里面的堆操作一般用到的只有4个:make_heap();、pop_heap();、push_heap();、sort_heap();

他们的头文件函数是#include <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]);

for(i=0;i<12;i++)

printf("%d ",number[i]);

printf("\n");

make_heap(&number[0],&number[12],cmp);

for(i=0;i<12;i++)

printf("%d ",number[i]);

printf("\n");

//加入元素8

number[12]=8;

//加入后调整

push_heap(&number[0],&number[13],cmp);

for(i=0;i<13;i++)

printf("%d ",number[i]);

printf("\n");

//弹出元素8

pop_heap(&number[0],&number[13],cmp);

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;

}

  1. #include <cstdio>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <functional>
  5. using namespace std;
  6. void PrintfVectorInt(vector<int> &vet)
  7. {
  8. for (vector<int>::iterator pos = vet.begin(); pos != vet.end(); pos++)
  9. printf("%d ", *pos);
  10. putchar('\n');
  11. }
  12. int main()
  13. {
  14. const int MAXN = 20;
  15. int a[MAXN];
  16. int i;
  17. for (i = 0; i < MAXN; ++i)
  18. a[i] = rand() % (MAXN * 2);
  19. //动态申请vector 并对vector建堆
  20. vector<int> *pvet = new vector<int>(40);
  21. pvet->assign(a, a + MAXN);
  22. //建堆
  23. make_heap(pvet->begin(), pvet->end());
  24. PrintfVectorInt(*pvet);
  25. //加入新数据 先在容器中加入,再调用push_heap()
  26. pvet->push_back(25);
  27. push_heap(pvet->begin(), pvet->end());
  28. PrintfVectorInt(*pvet);
  29. //删除数据  要先调用pop_heap(),再在容器中删除
  30. pop_heap(pvet->begin(), pvet->end());
  31. pvet->pop_back();
  32. pop_heap(pvet->begin(), pvet->end());
  33. pvet->pop_back();
  34. PrintfVectorInt(*pvet);
  35. //堆排序
  36. sort_heap(pvet->begin(), pvet->end());
  37. PrintfVectorInt(*pvet);
  38. delete pvet;
  39. return 0;
  40. }   
  41.   

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之heap使用简介

    STL中并没有把heap作为一种容器组件,heap的实现亦需要更低一层的容器组件(诸如list,array,vector)作为其底层机制.Heap是一个类属算法,包含在algorithm头文件中.虽然 ...

  5. SGI STL泛型heap算法分析

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

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

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

  7. STL之heap学习

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

  8. STL中heap用法

    #include<cstdio> #include<iostream> #include<algorithm> using namespace std; ]={,, ...

  9. STL heap usage

    简介 heap有查找时间复杂度O(1),查找.插入.删除时间复杂度为O(logN)的特性,STL中heap相关的操作如下: make_heap() push_heap() pop_heap() sor ...

随机推荐

  1. 第四章:更多的bash shell命令

    第四章:更多的bash shell命令 监测程序 ps (其他ps内容见#1 ) Unix风格的ps命令参数 参数 描述 -A 显示所有进程 -N 显示与指定参数不符的所有进程 -a 显示除控制进程( ...

  2. bzoj 3732 Network(最短路+倍增 | LCT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3732 [题意] 给定一个无向图,处理若干询问:uv路径上最长的边最小是多少? [思路一 ...

  3. Android - LayoutInflater

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  4. 《一步一步写嵌入式操作系统》读书笔记1—Skyeye介绍、安装和HelloWorld

    2013-11-14 最近在看<一步一步写嵌入式操作系统>,感觉此书甚好,许多地方讲得很清楚.可操作性强,计划边读边实践边写笔记,希望能够逐步熟悉嵌入式操作系统底层的东西,最终剪裁出一套实 ...

  5. sys.check_constraints

    每个用作 CHECK 约束(sys.objects.type = C)的对象都在表中占一行. SELECT name FROM sys.check_constraints -- equal to SE ...

  6. dfs.datanode.max.xcievers参数导致hbase集群报错

    2013/08/09 转发自http://bkeep.blog.163.com/blog/static/123414290201272644422987/ [案例]dfs.datanode.max.x ...

  7. linux 配置免密码登录

    主要就是两步 : 1. scp ~/.ssh/id_rsa.pub root@远程ip地址:~/ 2. cat id_rsa.pub >> ~/.ssh/authorized_keys,把 ...

  8. redis的hashes类型

    redis hash 是一个string类型的field和value 的映射表.它的添加.删除操作都是O(1) . hash特别适合用于存储对象.相较于将对象的每个字段存成单个string类型 . 将 ...

  9. Android“This Handler class should be static or leaks might occur”警告的处理方法

    此文属于转载! 最近用到handle在线程中改变UI,会跟给出“This Handler class should be static or leaks might occur”的警告,网上看了很多解 ...

  10. homework-08

    这次作业是考察关于C++的一些使用,由于我的C++只掌握了基本功,所以我只有霸王硬上弓,勉强写写自己的浅见. 1. 理解C++变量的作用域和生命周期 对一个C++变量来说,有两个属性非常重要:作用域和 ...