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 ...
随机推荐
- 【NOIP模拟题】Permutation(dp+高精度)
首先我们可以这样想: 设状态f[i, j]表示1-i序列有j个'<'的方案数 那么考虑转移 因为i比i-1大,所以可以考虑从i-1来转移.首先i是要插入1-i-1这个序列的,所以我们可以思考插入 ...
- ThinkPHP整合cropper剪裁图片上传功能
1.先下载核心文件:https://github.com/fengyuanchen/cropper 2. 3.对于index.html文件 4.对于main.js文件 5.对于crop.php文件 & ...
- ubuntu 16.04安装 navicat
原文地址:http://www.cnblogs.com/wbJson/p/5655537.html 下载地址:http://download2.navicat.com/download/navicat ...
- 【SSH】远程下载
scp -r root@123.67.11.93:/var/lamp/abc.txt ./
- python2迁移python3的问题
▌使用 pathlib 模块来更好地处理路径 pathlib 是 Python 3默认的用于处理数据路径的模块,它能够帮助我们避免使用大量的 os.path.joins语句: from pathlib ...
- Codeforces 417 D. Cunning Gena
按monitor排序,然后状压DP... . D. Cunning Gena time limit per test 1 second memory limit per test 256 megaby ...
- android最新版 极光推送
极光推送对于移动开发的程序员都不陌生,用起来也挺方便的,今天在这里给大家介绍下最先版的极光推送的用法,超级简单. 1.在build.gradle里面添加两个方法并引用一个库文件 1.1在default ...
- 自定义View中的Path
我们用Path可以画返回图标,可以画搜索图标,也可以画一个圆,DIDI
- 使用 awk 过滤文本或文件中的字符串
当我们在 Unix/Linux 下使用特定的命令从字符串或文件中读取或编辑文本时,我们经常需要过滤输出以得到感兴趣的部分.这时正则表达式就派上用场了. 什么是正则表达式? 正则表达式可以定义为代表若干 ...
- 后端程序员如何玩转AJAX
1.原生的Ajax入门 (感觉很是繁琐,所以一般我们都是用简单的方式) 创建一个核心对象 XMLHttpRequest var xmlhttp; if (window.XMLHttpRequest) ...