在C++中,虽然堆不像 vector, set 之类的有已经实现的数据结构,但是在 algorithm.h 中实现了一些相关的模板函数。下面是一些示例应用

http://www.cplusplus.com/reference/algorithm/pop_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
}

  

heap c++ 操作 大顶堆、小顶堆的更多相关文章

  1. Java大顶和小顶

    http://blog.sina.com.cn/s/blog_651c9a360100o7y1.html http://blog.csdn.net/cnbird2008/article/details ...

  2. 堆排序(大顶堆、小顶堆)----C语言

    堆排序 之前的随笔写了栈(顺序栈.链式栈).队列(循环队列.链式队列).链表.二叉树,这次随笔来写堆 1.什么是堆? 堆是一种非线性结构,(本篇随笔主要分析堆的数组实现)可以把堆看作一个数组,也可以被 ...

  3. Python使用heapq实现小顶堆(TopK大)、大顶堆(BtmK小)

    Python使用heapq实现小顶堆(TopK大).大顶堆(BtmK小) | 四号程序员 Python使用heapq实现小顶堆(TopK大).大顶堆(BtmK小) 4 Replies 需1求:给出N长 ...

  4. 大顶堆与小顶堆应用---寻找前k小数

    vector<int> getLeastNumber(vector<int>& arr,int k){ vector<int> vec(k,); if(== ...

  5. 数据结构:堆排序 (python版) 小顶堆实现从大到小排序 | 大顶堆实现从小到大排序

    #!/usr/bin/env python # -*- coding:utf-8 -*- ''' Author: Minion-Xu 小堆序实现从大到小排序,大堆序实现从小到大排序 重点的地方:小堆序 ...

  6. 《排序算法》——堆排序(大顶堆,小顶堆,Java)

    十大算法之堆排序: 堆的定义例如以下: n个元素的序列{k0,k1,...,ki,-,k(n-1)}当且仅当满足下关系时,称之为堆. " ki<=k2i,ki<=k2i+1;或k ...

  7. 剑指offer:数据流中的中位数(小顶堆+大顶堆)

    1. 题目描述 /** 如何得到一个数据流中的中位数? 如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值. 如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两 ...

  8. 378. Kth Smallest Element in a Sorted Matrix(大顶堆、小顶堆)

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  9. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. C#开发微信公众平台(附Demo)

    服务号和订阅号 服务号是公司申请的微信公共账号,订阅号是个人申请的,我个人也申请了一个,不过没怎么用. 服务号 1个月(30天)内仅可以发送1条群发消息. 发给订阅用户(粉丝)的消息,会显示在对方的聊 ...

  2. HDOJ(2438)几何里的三分

    Turn the corner http://acm.hdu.edu.cn/showproblem.php?pid=2438 题目:一辆车能否在一个路口拐弯,看图就很明白啦. 算法:见下图,只要求出图 ...

  3. RequireJS基础(三)

    这篇来写一个具有依赖的事件模块event. event提供三个方法bind.unbind.trigger来管理DOM元素事件. event依赖于cache模块,cache模块类似于jQuery的$.d ...

  4. Activex、OLE、COM、OCX、DLL之间的区别(转)

    熟悉面向对象编程和网络编程的人一定对ActiveX.OLE和COM/DCOM这些概念不会陌生,但是它们之间究竟是什么样的关系,对许多们还是比较模糊的.在具体介绍它们的关系之间,我们还是先明确组件(Co ...

  5. DGV表头双行

    try { if (e.RowIndex < 0) { RectangleF _rect = e.CellBounds; Pen _pen = new Pen(Color.Black); Pen ...

  6. String easy 结束日

    (1)Compare Version Numbers 解题思路:把字符串分割成字符串数组,然后取两个字符串数组的最大长度,从数组的第一个元素开始比较,注意把String型转换成Int型(Integer ...

  7. jsp 访问jdbc实例

    前提条件: 1.安装Java,tomcat,MySQL,eclipse 2.配置java 环境变量 3.配置MySQL参数 4.tomcat能正常启动 使用eclipse编写代码: 1. 新建一个we ...

  8. C# 以管理员身份运行WinForm程序

    最近帮客户开发的WinForm客户端,部分在使用的过程中,会出现“系统文件找不到”的错误提示. 调试后,确定为程序在操作配置文件时,系统权限引起的错误,直接管理员权限运行就正常了. 考虑用户操作的便利 ...

  9. Varnish简介

    Varnish介绍: Varnish是一个反向HTTP代理,有时也被称为HTTP的加速器或网络加速器:它存在于真实服务器的前面(可能有多级代理),将来自于客户端的请求中的部分内容存储在自身的内存中,以 ...

  10. maven项目报:An error occurred while filtering resources

    maven项目在problem中报: An error occurred while filtering resources   解决方法: 右键项目-maven-update project..