STL之heap
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;
}
- #include <cstdio>
- #include <vector>
- #include <algorithm>
- #include <functional>
- using namespace std;
- void PrintfVectorInt(vector<int> &vet)
- {
- for (vector<int>::iterator pos = vet.begin(); pos != vet.end(); pos++)
- printf("%d ", *pos);
- putchar('\n');
- }
- int main()
- {
- const int MAXN = 20;
- int a[MAXN];
- int i;
- for (i = 0; i < MAXN; ++i)
- a[i] = rand() % (MAXN * 2);
- //动态申请vector 并对vector建堆
- vector<int> *pvet = new vector<int>(40);
- pvet->assign(a, a + MAXN);
- //建堆
- make_heap(pvet->begin(), pvet->end());
- PrintfVectorInt(*pvet);
- //加入新数据 先在容器中加入,再调用push_heap()
- pvet->push_back(25);
- push_heap(pvet->begin(), pvet->end());
- PrintfVectorInt(*pvet);
- //删除数据 要先调用pop_heap(),再在容器中删除
- pop_heap(pvet->begin(), pvet->end());
- pvet->pop_back();
- pop_heap(pvet->begin(), pvet->end());
- pvet->pop_back();
- PrintfVectorInt(*pvet);
- //堆排序
- sort_heap(pvet->begin(), pvet->end());
- PrintfVectorInt(*pvet);
- delete pvet;
- return 0;
- }
STL之heap的更多相关文章
- L2-012. 关于堆的判断(STL中heap)
L2-012. 关于堆的判断 将一系列给定数字顺序插入一个初始为空的小顶堆H[].随后判断一系列相关命题是否为真.命题分下列几种: “x is the root”:x是根结点: “x and y ...
- STL之heap与优先级队列Priority Queue详解
一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的m ...
- STL中heap相关函数
heap并不是属于STL中的containers,而是在<algorithm>下提供了相关的函数 make_heap,sort_heap,pop_heap,push_heap 函数的说明: ...
- STL之heap使用简介
STL中并没有把heap作为一种容器组件,heap的实现亦需要更低一层的容器组件(诸如list,array,vector)作为其底层机制.Heap是一个类属算法,包含在algorithm头文件中.虽然 ...
- SGI STL泛型heap算法分析
heap性质 heap本质是用一个数组表示的完全二叉树,并且父节点总是大于(或者小于)子节点的值.在STL中用于实现优先队列(priority_queque).堆排序是排序算法中是稳定效率最高的一种. ...
- STL中heap算法(堆算法)
①push_heap算法 以下是push_heap算法的实现细节.该函数接收两个迭代器,用来表现一个heap底部容器(vector)的头尾,而且新元素已经插入究竟部的最尾端. template ...
- STL之heap学习
C++标准库中的堆-heap make_heap函数,包括两个参数(begin(number),end(number)).(左闭右开) pop_heap函数,包括两个参数,起始位置和终止位置,将当前区 ...
- STL中heap用法
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; ]={,, ...
- STL heap usage
简介 heap有查找时间复杂度O(1),查找.插入.删除时间复杂度为O(logN)的特性,STL中heap相关的操作如下: make_heap() push_heap() pop_heap() sor ...
随机推荐
- 当rsync遇到非默认端口的ssh
在使用rsync使用ssh协议,来同步远程文件的方法,rsync -zvrtopg -e ssh但是如果遇到ssh不是22端口的时候使用rsync -zvrtopg -e ‘ssh -p 端口’特别是 ...
- [Hive - Tutorial] Creating, Showing, Altering, and Dropping Tables
Creating, Showing, Altering, and Dropping Tables See Hive Data Definition Language for detailed info ...
- 数位DP专题
这周开始刷数位DP,在网上找到一份神级数位DP模板,做起题目来爽歪歪. http://www.cnblogs.com/jffifa/archive/2012/08/17/2644847.html in ...
- linux 命令 之chomd
chmod用于改变文件或目录的访问权限.用户用它控制文件或目录的访问权限.该命令有两种用法.一种是包含字母和操作符表达式的文字设定法:另一种是包含数字的数字设定法. 1. 文字设定法 语法:chmo ...
- 在线的JSON formate工具
一个非常好的json formate工具 可以很容易发现json的错误,以及对json进行格式化 https://jsonformatter.curiousconcept.com/
- zookeeper的配置项
1 tickTime:CS通信心跳数 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳.tickTime以毫秒为单位. tick ...
- Lua学习笔记(一):搭建开发环境
Lua是一个小巧高效的解释型脚本语言,可以方便的嵌入到任意的语言中,很多应用程序.游戏使用LUA作为自己的嵌入式脚本语言,以此来实现可配置性.可扩展性.这其中包括魔兽世界.博德之门.愤怒的小鸟.VOC ...
- iOS UIWebView加载时添加进度条
标注:此框架仅适合UIWebView 对iOS8后新出的WKWebView不适用,当然,你可以尝试修改框架里的几个代理方法. 框架是:NJKWebViewProgress 导入头文件 #import ...
- MongoDB 快速入门--高级
引用 --------->DBRefs DBRef的形式: { $ref : , $id : , $db : } $ref:集合名称 $id:引用的id $db:数据库名称,可选参数 { &qu ...
- Node.js和mybatis分别实现mysql中like变量模糊查询
<!-- mybatis --> <where> <if test="varName != '' and varName != null" > ...