在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. 寻找 nani (1)

    test.pl文件room(kitchen). room(office). room(hall). room('dining room'). room(cellar). location(desk,o ...

  2. echarts引入及应用

    1.在官网上下载echarts并引入项目中 <script src="js/echarts.js"></script> 2.给一个DOM作为图表展示的容器, ...

  3. 【英语魔法俱乐部——读书笔记】 3 高级句型-简化从句&倒装句(Reduced Clauses、Inverted Sentences) 【完结】

    [英语魔法俱乐部——读书笔记] 3 高级句型-简化从句&倒装句(Reduced Clauses.Inverted Sentences):(3.1)从属从句简化的通则.(3.2)形容词从句简化. ...

  4. yum源使用(转)

    一.yum 简介 yum,是Yellow dog Updater, Modified 的简称,是杜克大学为了提高RPM 软件包安装性而开发的一种软件包管理器.起初是由yellow dog 这一发行版的 ...

  5. [原创.数据可视化系列之六]使用openlyaers进行公网地图剪切

    进行地图开发的过程中,我一般使用天地图或者微软的地图作为地图,因为这两种地图的经纬度偏差最小,基本可以满足用户需求,比如: 不用说,都是全部地图,这也是最常用的一种方法. 但是用户说,我只看大连的地图 ...

  6. Linux QT

    Check system version 1.  cat /proc/version mike@sp-ThinkPad-X220:~$ cat /proc/versionLinux version 3 ...

  7. 数据库imp导表dmp的方法

    1>sqlplus / as sysdba 进入sqlplus 2>drop user USER cascade 3>create user USER IDENTIFIED BY P ...

  8. 试一下SVG

    用鼠标点击那个圆形试试 function changeCircle() { var c = document.getElementById("mycircle"); c.setAt ...

  9. linux命令(7):mv命令

    mv命令 mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录. 1.命令格式: mv [选项] ...

  10. 初探C++Primer(15.面向对象程序设计)

    最近在恶补OOP相关知识,很遗憾学校的课没选上,于是只能上网购进C++Primer一本,开始重学C++之旅... (壮哉我大ZJU,网购半天到货XDD) 学习路线 7.类->13.类设计者的工具 ...