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)是一个有效的堆,如果不是有效堆就老老实实用make_heap()

然后,再把堆中的新元素加进来,做成一个堆。


最后是sort_heap()void
sort_heap(first_pointer,end_pointer,compare_function);

作用是sort_heap对[first,last)中的序列进行排序。它假设这个序列是有效堆。(当然
,经过排序之后就不是一个有效堆了)

下面是一个数组heap的例子

 #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 29 15 26 22 23 51 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 8
for(i=;i<;i++)
{
printf("%d ",number[i]);
}
printf("\n"); sort_heap(&number[],&number[],cmp); //降序
//结果不用说都知道是有序的了!
for(i=;i<;i++)
{
printf("%d ",number[i]);
}
return ;
}

下面是一个vector heap的例子

 #include <iostream>
#include <algorithm>
#include <vector>
using namespace std; const int VECTOR_SIZE = ; int main()
{
vector<int, allocator<int> >Numbers(VECTOR_SIZE) ;
vector<int, allocator<int> >::iterator it ; // Initialize vector Numbers
Numbers[] = ;
Numbers[] = ;
Numbers[] = ;
Numbers[] = ;
Numbers[] = ;
Numbers[] = ;
Numbers[] = ;
Numbers[] = ; // print content of Numbers
cout << "Numbers { " ;
for(it = Numbers.begin(); it != Numbers.end(); it++)
{
cout << *it << " " ;
}
cout << " }\n" << endl ; // convert Numbers into a heap
make_heap(Numbers.begin(), Numbers.end()) ; //缺省参数生成大根堆 cout << "After calling make_heap\n" << endl ;
// print content of Numbers
cout << "Numbers { " ;
for(it = Numbers.begin(); it != Numbers.end(); it++)
{
cout << *it << " " ;
}
cout << " }\n" << endl ; // sort the heapified sequence Numbers
sort_heap(Numbers.begin(), Numbers.end()) ; //缺省参数默认升序排列 cout << "After calling sort_heap\n" << endl ;
// print content of Numbers
cout << "Numbers { " ;
for(it = Numbers.begin(); it != Numbers.end(); it++)
{
cout << *it << " " ;
}
cout << " }\n" << endl ; //insert an element in the heap
Numbers.push_back() ; //在堆的尾部添加一个数据
//push_heap(Numbers.begin(), Numbers.end()) ; //因为之前使用sort对堆进行了排序,所以
//[first,last-1)不是一个有效的堆,push_heap没效果
// you need to call make_heap to re-assert the
// heap property
make_heap(Numbers.begin(), Numbers.end()) ; //使用这个直接全部重新生成新堆 cout << "After calling push_heap and make_heap\n" << endl ; // print content of Numbers
cout << "Numbers { " ;
for(it = Numbers.begin(); it != Numbers.end(); it++)
{
cout << *it << " " ;
}
cout << " }\n" << endl ; // remove the root element from the heap Numbers
pop_heap(Numbers.begin(), Numbers.end()) ; cout << "After calling pop_heap\n" << endl ; // print content of Numbers
cout << "Numbers { " ;
for(it = Numbers.begin(); it != Numbers.end(); it++)
{
cout << *it << " " ;
}
cout << " }\n" << endl ;
return ;
}
/*
程序输出为: Numbers { 4 10 70 10 30 69 96 100 }
之后调用 make_heap Numbers { 100 30 96 10 4 69 70 10 }
之后调用 sort_heap Numbers { 4 10 10 30 69 70 96 100 }
之后调用 push_heap 和 make_heap Numbers { 100 69 96 30 4 70 10 10 7 }
之后调用 pop_heap Numbers { 96 69 70 30 4 7 10 10 100 }
*/

STL——heap的4大操作的更多相关文章

  1. STL -- heap结构及算法

    STL -- heap结构及算法 heap(隐式表述,implicit representation) 1. heap概述 : vector + heap算法 heap并不归属于STL容器组件,它是个 ...

  2. [STL]heap和priority_queue

    一.heap 在STL中,priority_queue(优先权队列)的底层机制是最大堆,因此有必要先来了解一下heap.heap采用完全二叉树的结构,当然不是真正的binary tree,因为对于完全 ...

  3. POJ 2442 Squence (STL heap)

    题意: 给你n*m的矩阵,然后每行取一个元素,组成一个包含n个元素的序列,一共有n^m种序列, 让你求出序列和最小的前n个序列的序列和. 解题思路: 1.将第一序列读入seq1向量中,并按升序排序. ...

  4. STL——heap结构及算法

    heap(隐式表述,implicit representation) 1. heap概述 : vector + heap算法 heap并不归属于STL容器组件,它是个幕后英雄,扮演priority q ...

  5. 【算法学习】老算法,新姿势,STL——Heap

    “堆”是一个大家很熟悉的数据结构,它可以在\(O(log\;n)\)的时间内维护集合的极值. 这都是老套路了,具体的内部实现我也就不谈了. 我一般来说,都是用queue库中的priority_queu ...

  6. STL heap部分源代码分析

    本文假设你已对堆排序的算法有主要的了解. 要分析stl中heap的源代码的独到之处.最好的办法就是拿普通的代码进行比較.话不多说,先看一段普通的堆排序的代码: //调整大顶堆.使得结构合理 void ...

  7. STL~heap

    1.定义 堆:若将此序列所存储的向量R[1..n]看做是一棵完全二叉树的存储结构,则堆实质上是满足如下性质的完全二叉树 树中任一非叶子结点的关键字均不大于(或不小于)其子结点的关键字.分为大根数(默认 ...

  8. STL heap usage

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

  9. STL中常用容器及操作 学习笔记1

    @[TOC](下面介绍STL中常见的容器及操作)## 不定长数组 vector> vetcor:其实就是一个数组或者说是容器 其操作不同于之前直接定义的数组 > 而且可以直接赋值也可以直接 ...

随机推荐

  1. 自定义控件(视图)2期笔记09:自定义视图之继承自ViewGroup(仿ViewPager效果案例)

    1. 这里我们继承已有ViewGroup实现自定义控件,模拟出来ViewPager的效果,如下: (1)实现的效果图如下: (2)实现步骤: • 自定义view继承viewGroup • 重写onLa ...

  2. codevs1955光纤通信(并查集)

    /* 第一眼以为就是个区间覆盖 然后敲完提交60分0.0 然而觉得自己的做法很对 以为数据错了 后来发现XXX他的牛棚是一圈(牛过得挺好的啊 还能赏湖...) 然后枚举断开的点 可惜n=750 p=1 ...

  3. Word 查找替换,通配符一览表

    Word查找替换详细用法及通配符一览表 使用通配符要查找“?”或者“*”,可输入“\?”和“\*”,\1\2\3依次匹配数对括号内容查找(a)12(b)   替换\2XY\1   结果:bXYa ([ ...

  4. My.Ioc 代码示例——注册项的注销和更新

    当您需要从 Ioc 容器中注销/删除一个注册项的时候,您会怎么做呢? 有人曾经在 stackoverflow 上提问“如何从 Unity 中注销一个注册项”.对于这个问题,有人的回答是“有趣.你为什么 ...

  5. Web字体库下载及转换工具

    1.字体现在网站: http://ztxz.org/ 2.Web字体在线格式转换器: http://www.freefontconverter.com/ 3.

  6. Access数据库数据转换Table.Json

    使用WPF组件 xaml <Window x:Class="JsonConvert.MainWindow" xmlns="http://schemas.micros ...

  7. Lucene.net 从创建索引到搜索的代码范例

    关于Lucene.Net的介绍网上已经很多了在这里就不多介绍Lucene.Net主要分为建立索引,维护索引和搜索索引Field.Store的作用是通过全文检查就能返回对应的内容,而不必再通过id去DB ...

  8. 人民币符号¥在css和html正确显示

    商城项目需要涉及到人民币的页面现实问题.但是¥(指的是通常输入法中文全角模式下按shift+4的那个)在宋体(v3.03, v5.0)的情况下是显示一杠.常见的其他字体微软雅黑(Microsoft Y ...

  9. 07_RHEL7配置yum源

    redhat 默认自带的 yum 源需要注册才能更新.想不花钱也可以更新,就需要替换掉redhat的yum源. 检查是否安装yum包 查看RHEL是否安装了yum,若是安装了,那么又有哪些yum包: ...

  10. underscorejs-sortBy学习

    2.17 sortBy 2.17.1 语法 _.sortBy(list, iteratee, [context]) 2.17.2 说明 返回一个排序后的list拷贝副本. list为集合,如数组.对象 ...