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 ...
随机推荐
- 关于Unity中的.meta文件
.meta文件是用于辅助管理Unity资源文件的文件,删除后,Unity会自动生成,里面记录了各个资源Inspector的信息,属性等等,Unity是不会改变源资源文件的,没有意义,它是靠.meta文 ...
- CI -- $this->load->library()详解
我第一次加载失败,原来是文件名和类名不同的原因,先总结关于CI加载你自己的类文件注意事项: 1.第三方加载文件应放在application/libraries文件下 2.文件名和类名应该相同,并且首字 ...
- bootstrap基础学习七篇
bootstrap图片 Bootstrap 提供了三个可对图片应用简单样式的 class: .img-rounded:添加 border-radius:6px 来获得图片圆角. .img-circle ...
- 求出每个team粉丝数最多的3个国家
有这么个表 fans(team,nationality,fanCount) 'Barcelona','Germany',12000'Barcelona','Spain',18000'Barcelona ...
- LINQ to SQL语句(2)Count/Sum/Min/Max/Avg操作符
使用场景 类似于SQL中的聚合函数,用于统计数据,不延迟.如返回序列中的元素数量.求和.最小值.最大值.求平均值. Count 说明:用于返回集合中元素的个数,返回Int类型,生成SQL语句为SELE ...
- ios开发之 -- x-code删除描述文件
描述文件所在的目录是:~/Library/MobileDevice/Provisioning\ Profiles/ 进入这个目录,删除所有描述文件.
- poj_1836 动态规划
题目大意 N个士兵排成一排,不是按照高度顺序排列.现在想要从中去掉几名士兵,从而使得队伍中剩余的士兵能够看到这排最左边或者最右边的那个士兵,某士兵能够看到最左边(或最右边)的士兵指这名士兵和最左边(或 ...
- Objective-C代码学习大纲(6)
2011-05-11 14:06 佚名 otierney 字号:T | T 本文为台湾出版的<Objective-C学习大纲>的翻译文档,系统介绍了Objective-C代码,很多名词为台 ...
- 《从零开始学Swift》学习笔记(Day 66)——Cocoa Touch设计模式及应用之通知机制
原创文章,欢迎转载.转载请注明:关东升的博客 通知(Notification)机制是基于观察者(Observer)模式也叫发布/订阅(Publish/Subscribe)模式,是 MVC( 模型-视图 ...
- kindeditor在Java项目中的应用以及图片上传配置
在官网下载Kindededitor的开发包 在项目中javaweb项目中导入kindeditor必须要使用的Jar包(用于文件上传,除非你的富文本编辑器不使用图片上传)jar包可以在官网的开发包中 ...