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 ...
随机推荐
- 在WinForm编程中犯的一些错误
1.一直以为,MouseClick事件在鼠标点击时发生,MouseDoubleClick事件在鼠标双击时发生.那么在单击鼠标时会调用MouseClick事件处理程序,双击鼠标时会调用MouseDoub ...
- HDU 5792 World is Exploding (树状数组)
World is Exploding 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...
- POJ 1789 Truck History (最小生成树)
Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...
- UVALive 6910 Cutting Tree(离线逆序并查集)
[题目]:(地址:) http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97671#problem/E [题意]: 给出多棵树和两类操作:操作 ...
- codeforce 621A(水题)
A. Wet Shark and Odd and Even time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- 关于cocoapods添加静态库的奇葩配置
不多说,直接上代码 当引入这个静态库时,一开始死活在编辑时找不到这个静态库. 直到看到这个贴子:http://stackoverflow.com/questions/19189463/cocoapod ...
- oracle对序列的操作
select t.*, t.rowid from tbl_type t order by t.id desc Select SEQ_TBL_TYPE_ID.NextVal From Dual; ; ; ...
- Egret项目Typescript的编译报错
今天编译项目,出现了一个奇怪的报错,如下: E:\engine\egret-core-3.1.2\tools\lib\typescript\tsclark.js:41531 1> if (fil ...
- SQL 错误1418
1.一个或多个服务器网络地址缺少完全限定域名(FQDN).为每个服务器指定FQDN,然后再次单击“开始镜像”.2.服务器网络地址"TCP://primary.test.com:5022&qu ...
- android 多线程数据库读写分析与优化
最新需要给软件做数据库读写方面的优化,之前无论读写,都是用一个 SQLiteOpenHelper.getWriteableDataBase() 来操作数据库,现在需要多线程并发读写,项目用的是2.2的 ...