STL make_heap push_heap pop_heap sort_heap
make_heap:
default (1) |
template <class RandomAccessIterator> |
---|---|
custom (2) |
template <class RandomAccessIterator, class Compare> |
Rearranges the elements in the range [first,last)
in such a way that they form a heap.
A heap is a way to organize the elements of a range that allows for fast retrieval of the element with the highest value at any moment (with pop_heap), even repeatedly, while allowing for fast insertion of new elements (with push_heap).
The element with the highest value is always pointed by first. The order of the other elements depends on the particular implementation, but it is consistent throughout all heap-related functions of this header.
The elements are compared using operator<
(for the first version), or comp (for the second): The element with the highest value is an element for which this would return false
when compared to every other element in the range.
The standard container adaptor priority_queue calls make_heap, push_heap and pop_heap automatically to maintain heap properties for a container.
comp:
Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool
. The value returned indicates whether the element passed as first argument is considered to be less than the second in the specific strict weak ordering it defines.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.
make_heap(_First, _Last, _Comp)
默认是建立最大堆的。对int类型,可以在第三个参数传入greater<int>()得到最小堆。
int A[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
make_heap(A, A + 10);
for(int i=0;i<10;i++)
cout<<A[i]<<" ";
输出:9 8 6 7 4 5 2 0 3 1
vector<int> B(A,A+10);
make_heap(B.begin(),B.end(),greater<int>());//min-heap
cout<<B[0]<<endl;
输出0.
pop_heap(B.begin(),B.end()); B.pop_back();
cout<<B[9]<<endl;
可以看到,pop_heap会把堆顶元素放到序列末尾,即b.back()处。
// range heap example
#include <iostream> // std::cout
#include <algorithm> // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap
#include <vector> // std::vector int main () {
int myints[] = {,,,,};
std::vector<int> v(myints,myints+); std::make_heap (v.begin(),v.end());
std::cout << "initial max heap : " << v.front() << '\n'; std::pop_heap (v.begin(),v.end()); v.pop_back();
std::cout << "max heap after pop : " << v.front() << '\n'; v.push_back(); std::push_heap (v.begin(),v.end());
std::cout << "max heap after push: " << v.front() << '\n'; std::sort_heap (v.begin(),v.end()); std::cout << "final sorted range :";
for (unsigned i=; i<v.size(); i++)
std::cout << ' ' << v[i]; std::cout << '\n'; return ;
}
输出:
initial max heap : 30
max heap after pop : 20
max heap after push: 99
final sorted range : 5 10 15 20 99
在堆中添加数据
push_heap (_First, _Last)
要先在容器中加入数据,再调用push_heap ()
在堆中删除数据
pop_heap(_First, _Last)
要先调用pop_heap()再在容器中删除数据
堆排序
sort_heap(_First, _Last)
排序之后就不再是一个合法的heap了
陈硕 内存的多路归并排序:
https://github.com/chenshuo/recipes/blob/master/algorithm/mergeN.cc
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector> typedef int Record;
typedef std::vector<Record> File; struct Input
{
Record value;
size_t index;
const File* file; explicit Input(const File* f)
: value(-),
index(),
file(f)
{ } bool next()
{
if (index < file->size())
{ value = (*file)[index];
++index;
return true;
} else {
return false;
}
} bool operator<(const Input& rhs) const
{
// make_heap to build min-heap, for merging
return value > rhs.value;
}
}; File mergeN(const std::vector<File>& files)
{
File output;
std::vector<Input> inputs; for (size_t i = ; i < files.size(); ++i) {
Input input(&files[i]);
if (input.next()) {
inputs.push_back(input);
}
} std::make_heap(inputs.begin(), inputs.end());
while (!inputs.empty()) {
std::pop_heap(inputs.begin(), inputs.end());
output.push_back(inputs.back().value); if (inputs.back().next()) {
std::push_heap(inputs.begin(), inputs.end());
} else {
inputs.pop_back();
}
} return output;
} int main()
{
const int kFiles = ;
std::vector<File> files(kFiles);
for (int i = ; i < kFiles; ++i) {
File file(rand() % );
std::generate(file.begin(), file.end(), &rand);
std::sort(file.begin(), file.end());
files[i].swap(file);
} File output = mergeN(files); std::copy(output.begin(), output.end(),
std::ostream_iterator<Record>(std::cout, "\n"));
}
STL make_heap push_heap pop_heap sort_heap的更多相关文章
- 堆的操作(make_heap,push_heap,pop_heap,sort_heap,is_heap)
堆不是一中sort ranges,堆中的元素不会以递增方式排列,内部以树状形式排列,该结构以每个结点小于等于父节点构成,优先队列就是以堆来实现 make_heap //版本一:用operator &l ...
- 不要重复发明轮子-C++STL
闫常友 著. 中国铁道出版社. 2013/5 标准的C++模板库,是算法和其他一些标准组件的集合. . 1.类模板简介 2.c++中的字符串 3.容器 4.c++中的算法 5.迭代器 6.STL 数值 ...
- 论C++STL源代码中关于堆算法的那些事
关于堆,我们肯定熟知的就是它排序的时间复杂度在几个排序算法里面算是比較靠上的O(nlogn)常常会拿来和高速排序和归并排序讨论,并且它还有个长处是它的空间复杂度为O(1), 可是STL中没有给我们提供 ...
- STL--STL和她的小伙伴们:
STL--概述: 标准模板库(StandardTemplateLibrary,STL),是C++程序设计语言标准模板库.STL是由Alexander Stepanov.Meng Lee和David R ...
- POJ 3784.Running Median
2015-07-16 问题简述: 动态求取中位数的问题,输入一串数字,每输入第奇数个数时求取这些数的中位数. 原题链接:http://poj.org/problem?id=3784 解题思路: 求取中 ...
- 温故而知新—heap
堆:堆不是STL中的容器组件,堆有分为大根堆和小根堆,堆的底层实现可以用优先队列进行实现.底层的容器实际上是一个vector.在C++数据结构中,堆也可用数组来实现.对于使用C++的开发人员来说,st ...
- (转)c++一些知识点
异常详解: https://www.cnblogs.com/hdk1993/p/4357541.html#top 模版详解: https://blog.csdn.net/lezardfu/articl ...
- cb50a_c++_STL_算法_局部排序partial_sort
cb50a_c++_STL_算法_局部排序partial_sort partial_sort(b,se,e)排序一部分,begin,source end,endcout << " ...
- STL之heap与优先级队列Priority Queue详解
一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的m ...
随机推荐
- GitHub 上 57 款最流行的开源深度学习项目【转】
GitHub 上 57 款最流行的开源深度学习项目[转] 2017-02-19 20:09 334人阅读 评论(0) 收藏 举报 分类: deeplearning(28) from: https:// ...
- 第二百四十八节,Bootstrap轮播插件
Bootstrap轮播插件 学习要点: 1.轮播插件 本节课我们主要学习一下 Bootstrap 中的轮播插件. 一.轮播 轮播插件就是将几张同等大小的大图,按照顺序依次播放. 基本实例. 第一步,给 ...
- linux 常用命令总结(tsg)
1.解压tar.gz 第一步:gunzip filename.tar.gz --->会解压成.tar文件 第二步:tar xvf FileName.tar ---->会解压到当前文件夹2. ...
- m2014-c->c模拟java的hashmap容器类
转自:http://bbs.csdn.net/topics/390034346 在java中像ArrayList,HashMap都是现成的,在java.util包中,用的时候直接import java ...
- Excel 一个工作表进行按行数拆分
1. 如下Excel表,总共有120多行数据,如何将以50行数据为一个工作表进行拆分 Sub ZheFenSheet() Dim r, c, i, WJhangshu, WJshu, bt As Lo ...
- Struts2中的类型转换失败
类型转换失败: 若 Action 类没有实现 ValidationAware 接口: Struts 在遇到类型转换错误时仍会继续调用其 Action 方法, 就好像什么都没发生一样. 若 Action ...
- KVM虚拟机克隆及快照管理
一,克隆 查看虚拟机硬盘位置(其中centos1为虚拟机名称) virsh edit centos1 克隆(centos1为需要克隆的虚拟机名称centos2为克隆后的虚拟机名称CentOS2.qco ...
- Linux下查看mysql路径
ps -ef|grep mysql
- JavaWeb基于session和cookie的数据共享
在了解session和cookie技术之前,我们需要先了解一下什么是会话?会话可以简单理解为用户打开一个浏览器,点击多个超链接,访问服务器的多个web资源,然后关闭浏览器,整个过程称为一个会话.这样, ...
- Net 常用资源
opensource: http://www.dotnetfoundation.org/projects https://github.com/dotnet/corefx Enterprise Lib ...