heap c++ 操作 大顶堆、小顶堆
在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++ 操作 大顶堆、小顶堆的更多相关文章
- Java大顶和小顶
http://blog.sina.com.cn/s/blog_651c9a360100o7y1.html http://blog.csdn.net/cnbird2008/article/details ...
- 堆排序(大顶堆、小顶堆)----C语言
堆排序 之前的随笔写了栈(顺序栈.链式栈).队列(循环队列.链式队列).链表.二叉树,这次随笔来写堆 1.什么是堆? 堆是一种非线性结构,(本篇随笔主要分析堆的数组实现)可以把堆看作一个数组,也可以被 ...
- Python使用heapq实现小顶堆(TopK大)、大顶堆(BtmK小)
Python使用heapq实现小顶堆(TopK大).大顶堆(BtmK小) | 四号程序员 Python使用heapq实现小顶堆(TopK大).大顶堆(BtmK小) 4 Replies 需1求:给出N长 ...
- 大顶堆与小顶堆应用---寻找前k小数
vector<int> getLeastNumber(vector<int>& arr,int k){ vector<int> vec(k,); if(== ...
- 数据结构:堆排序 (python版) 小顶堆实现从大到小排序 | 大顶堆实现从小到大排序
#!/usr/bin/env python # -*- coding:utf-8 -*- ''' Author: Minion-Xu 小堆序实现从大到小排序,大堆序实现从小到大排序 重点的地方:小堆序 ...
- 《排序算法》——堆排序(大顶堆,小顶堆,Java)
十大算法之堆排序: 堆的定义例如以下: n个元素的序列{k0,k1,...,ki,-,k(n-1)}当且仅当满足下关系时,称之为堆. " ki<=k2i,ki<=k2i+1;或k ...
- 剑指offer:数据流中的中位数(小顶堆+大顶堆)
1. 题目描述 /** 如何得到一个数据流中的中位数? 如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值. 如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两 ...
- 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 ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
随机推荐
- npm库下载缓慢解决方案
淘宝针对国内下载npm库缓慢的问题,使用建立自己的cnpm库,可以很方便使用在公司内部使用. 地址:http://npm.taobao.org/ $ npm install -g cnpm --reg ...
- RHCA-红帽认证架构师
OpenStack: 向部署应用 配置域.数据源子系统.中实施应用安全 迁移应用至 介绍集群 考试代码 任选其中五门: (红帽企业虚拟化) (红帽企业部署和系统管理) (红帽企业服务器硬化) (红帽企 ...
- 9.5.8 Optimizing InnoDB Disk I/O
如果你数据库设计以及sq操作都是最佳实践,但是你数据库仍然被较重的io活动拖累的较慢,那么试一试看看top或者windows的任务管理器,cpu使用率和工作量低于70%,那么或许是您的硬盘较慢. 1 ...
- python-基础介绍
一.Linux基础 - 计算机以及日后我们开发的程序防止的服务器的简单操作 二.Python开发 http://www.cnblogs.com/wupeiqi/articles/5433893.htm ...
- VS2010 使用WebService
vs2010上面找不到直接创建webservice模板方式了.可以通过一下方式创建. 1.如果直接选择 .NET 4.0,可以选择 ASP.NET Empty Web Site/Application ...
- CSS3 结构伪类选择器 详解
1 CSS3 结构伪类选择器 1.E:root 匹配E元素所在的根元素 即:html 2. E:nth-child(n) (1)匹配E元素的父元素中第n个子元素,(2)且该位置的子元素类型必须是E类型 ...
- Windows 磁盘检查命令
今天在给朋友重装系统时,发现每次重启时总是出现下面的问题 上网搜索了下这个问题,可能的原因是:把磁盘格式转换成NTFS时没等转换完就重启或关机造成的. 解决方案:使用 "chkdsk/f D ...
- XML约束之DTD
XML文件的约束:什么叫约束呢?顾名思义,就是对xml文件的内容进行按照既定规则的限制.我们知道,因为xml文件的标签是可以自定义的,而往往我们用xml文件都是为了表达一定的数据集合(即小型的数据库) ...
- JavaScript内置对象之数组
一.JavaScript对象之数组 1.创建数组的方式 (1)使用Array构造函数 语法:new Array() 小括号()说明: -预先知道数组要保存的项目数量 -向Array构造函数中传递数组应 ...
- 一般html5 手机端头部需要
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...