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

而这个实现机制中的max-heap实际上是以一个vector表现的完全二叉树(complete binary tree)。
二叉堆(binary heap)就是i一种完全二叉树。也即是。整棵二叉树除了最底层的叶节点以外,都是填满的,而最低层的叶子结点必须是从左到右不留空隙。
至于max-heap和min-heap,前者的任何一个父亲结点都必须大于等于他的任意子结点,而后者相反。

《在这里值得一提的是》,heap对于map/unordered_map的排序(大小排序方式)是根据key值来排序的。

操作:

push_heap():新添加一个元素在末尾,然后重新调整堆序。也就是把元素添加在底层vector的end()处。

该算法必须是在一个已经满足堆序的条件下,添加元素。该函数接受两个随机迭代器,分别表示first,end,区间范围。

关键是我们执行一个siftup()函数,上溯函数来重新调整堆序。

pop_heap()这个算法跟push_heap类似,参数一样。不同的是我们把堆顶元素取出来,放到了数组或者是vector的末尾,用原来末尾元素去替代,然后end迭代器减1,执行siftdown()下溯函数来重新调整堆序。

注意算法执行完毕后,最大的元素并没有被取走,而是放于底层容器的末尾。如果要取走,则可以使用底部容器(vector)提供的pop_back()函数。

sort_heap()算法:既然每次pop_heap可以获得堆中最大的元素,那么我们持续对整个heap做pop_heap操作,每次将操作的范围向前缩减一个元素。

sort_heap() 算法:接受两个随机迭代器作为参数。表示操作的范围。

make_heap()算法:建立一个堆。很简单吧。接受的参数同上。

#include <iostream>
#include <algorithm> // make_heap(), pop_heap(), push_heap()
#include <vector>
using namespace std; void printVector(vector<int> &num)
{
for(int i = ; i < num.size(); i++)
cout<<num[i]<<" ";
cout<<endl;
}
int main()
{
// init
int arr[] = {,,,,,};
vector<int> num(arr,arr+);
printVector(num); // build
make_heap(num.begin(),num.end());
printVector(num); // 9 5 6 1 4 3 默认大顶堆 // get the biggest number
// 从vector的角度来取得
cout<<num[]<<endl; // 9
// or num.front();
cout<<num.front()<<endl; // 9 // delete 堆顶,即最大的元素
// 返回值为 void
// 将堆顶的元素放到最后一个位置上
// 弹出一个元素后,剩下的又重建了 heap,仍保持heap的性质
pop_heap(num.begin(),num.end());
printVector(num); // 6 5 3 1 4 9
// vector 删除末尾元素
num.pop_back();
printVector(num); num.push_back(); //首先在vector上扩容,增加一个元素到尾部
printVector(num); // 6 5 3 1 4 7
push_heap(num.begin(),num.end()); // 指定区间的最后一个元素加入堆中并使整个区间成为一个新的堆。注意前提是最后一个元素除外的所有元素已经构成一个堆。
printVector(num); // 7 5 6 1 4 3 // 判断是否为堆
bool ret = is_heap(num.begin(),num.end());
cout<<ret<<endl;
num.push_back();
printVector(num); // 7 5 6 1 4 3 9
cout<< is_heap(num.begin(),num.end()) <<endl;
push_heap(num.begin(),num.end());
printVector(num); // 9 5 7 1 4 3 6 sort_heap(num.begin(),num.end());
printVector(num); // 1 3 4 5 6 7 9
} // 小顶堆
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; class greater_class{
public:
bool operator()(int a, int b)
{
return a > b;
}
}; int main()
{
// init
int arr[] = {,,,,,};
vector<int> num(arr,arr+);
printVector(num); make_heap(num.begin(), num.end(), greater_class());
printVector(num); // 1 4 3 9 5 6 num.push_back();
printVector(num); // 1 4 3 9 5 6 2
push_heap(num.begin(),num.end(),greater_class());
printVector(num); // 1 4 2 9 5 6 3 while (num.size())
{
pop_heap(num.begin(),num.end(),greater_class());
long min = num.back();
num.pop_back();
cout << min << std::endl;
} // 1 2 3 4 5 6 9
}

c++ heap学习的更多相关文章

  1. STL之heap学习

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

  2. stl源码剖析 详细学习笔记heap

    // //  heap.cpp //  笔记 // //  Created by fam on 15/3/15. // // //---------------------------15/03/15 ...

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

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

  4. 【STL学习】堆相关算法详解与C++编程实现(Heap)

    转自:https://blog.csdn.net/xiajun07061225/article/details/8553808 堆简介   堆并不是STL的组件,但是经常充当着底层实现结构.比如优先级 ...

  5. ios学习路线—Objective-C(堆(heap)和栈(stack))

    Objective-C的对象在内存中是以堆的方式分配空间的,并且堆内存是由你释放的,即release 栈由编译器管理自动释放的,在方法中(函数体)定义的变量通常是在栈内,因此如果你的变量要跨函数的话就 ...

  6. 学习jvm,关于MAT an internal error occurred during:"Parsing heap dump" from问题

    写了一个死循环不断的创建对象,模拟内存溢出 package com.zuo.test1; import java.util.ArrayList; import java.util.List; publ ...

  7. Java再学习——栈(stack)和堆(heap)

    一.内存分配的策略 按照编译原理的观点,程序运行时的内存分配有三种策略,分别是静态的,栈式的,和堆式的. 静态存储分配是指在编译时就能确定每个数据目标在运行时刻的存储空间需求,因而在编译时就可以给他们 ...

  8. C++ STL源代码学习(map,set内部heap篇)

    stl_heap.h ///STL中使用的是大顶堆 /// Heap-manipulation functions: push_heap, pop_heap, make_heap, sort_heap ...

  9. 我的MYSQL学习心得(九) 索引

    我的MYSQL学习心得(九) 索引 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类 ...

随机推荐

  1. windows安装redis和PHP redis扩展

    1.安装Redis (1)这里选择在github官网上下载Redis,地址:Redis下载地址 下载压缩包(如下图),并解压到本地目录,我放在D:\wamp\redis\redis-windows ( ...

  2. asp.net多文件上传

    文件上传简单实现是非常容易的,但是想要更高的要求,比如通过ajax上传文件.一次上传多个文件.文件比较大等等,这里面的坑就不是很容易填(对于新手来说).因此在这里我准备通过ajax实现多文件上传.在开 ...

  3. python入门之实例-用户登录、注册

    用户密码存储文件db(其中用户和密码之间用$符合隔开): admin$123456 root$sdfk9f24 chy$654321 代码如下: def login(username,password ...

  4. css hack 笔记

    body{background-color:#000\9;}/*ie*/ body{background-color:#0f0\9\0;}/*ie9及以上*/ body{background-colo ...

  5. 通用全局CSS样式

    PC全局样式 *{padding:0;margin:0;} div,dl,dt,dd,form,h1,h2,h3,h4,h5,h6,img,ol,ul,li,table,th,td,p,span,a{ ...

  6. Android -Cannot run program "XXX/sdk/tools/emulator": error=2, No such file or directory

    I have installed android SDK and eclipse successfully on ubuntu 14.04. However,now it's not running. ...

  7. android开发学习 ------- @SuppressWarnings 注解的使用

    @SuppressWarnings 该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默. @SuppressWarnings 批注允许您选择性地取消特定代码段(即,类或方法 ...

  8. WPF CanExecuteChanged

    继承ICommand ,RelayCommand命令 public class RelayCommand : ICommand { private readonly Action _execute; ...

  9. 2189 数字三角形W

    2189 数字三角形W 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold       题目描述 Description 数字三角形要求走到最后mod 100最大 输入描述 ...

  10. html5.0学习记录(一)——可拖动视频播放器

    最近自己在重新学习html5新特性,了解到有视频标签和拖动标签,于是自己用这两个特性写了一个小demo,主要功能就是可以通过拖动视频来直接播放.效果图如下: 页面使用了<video>标签和 ...