make_heap:

default (1)
template <class RandomAccessIterator>
void make_heap (RandomAccessIterator first, RandomAccessIterator last);
custom (2)
template <class RandomAccessIterator, class Compare>
void make_heap (RandomAccessIterator first, RandomAccessIterator last,
Compare comp );
Make heap from range

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的更多相关文章

  1. 堆的操作(make_heap,push_heap,pop_heap,sort_heap,is_heap)

    堆不是一中sort ranges,堆中的元素不会以递增方式排列,内部以树状形式排列,该结构以每个结点小于等于父节点构成,优先队列就是以堆来实现 make_heap //版本一:用operator &l ...

  2. 不要重复发明轮子-C++STL

    闫常友 著. 中国铁道出版社. 2013/5 标准的C++模板库,是算法和其他一些标准组件的集合. . 1.类模板简介 2.c++中的字符串 3.容器 4.c++中的算法 5.迭代器 6.STL 数值 ...

  3. 论C++STL源代码中关于堆算法的那些事

    关于堆,我们肯定熟知的就是它排序的时间复杂度在几个排序算法里面算是比較靠上的O(nlogn)常常会拿来和高速排序和归并排序讨论,并且它还有个长处是它的空间复杂度为O(1), 可是STL中没有给我们提供 ...

  4. STL--STL和她的小伙伴们:

    STL--概述: 标准模板库(StandardTemplateLibrary,STL),是C++程序设计语言标准模板库.STL是由Alexander Stepanov.Meng Lee和David R ...

  5. POJ 3784.Running Median

    2015-07-16 问题简述: 动态求取中位数的问题,输入一串数字,每输入第奇数个数时求取这些数的中位数. 原题链接:http://poj.org/problem?id=3784 解题思路: 求取中 ...

  6. 温故而知新—heap

    堆:堆不是STL中的容器组件,堆有分为大根堆和小根堆,堆的底层实现可以用优先队列进行实现.底层的容器实际上是一个vector.在C++数据结构中,堆也可用数组来实现.对于使用C++的开发人员来说,st ...

  7. (转)c++一些知识点

    异常详解: https://www.cnblogs.com/hdk1993/p/4357541.html#top 模版详解: https://blog.csdn.net/lezardfu/articl ...

  8. cb50a_c++_STL_算法_局部排序partial_sort

    cb50a_c++_STL_算法_局部排序partial_sort partial_sort(b,se,e)排序一部分,begin,source end,endcout << " ...

  9. STL之heap与优先级队列Priority Queue详解

    一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的m ...

随机推荐

  1. ie设置ActiveX控件不提示

    ie设置自动允许activex: 对安全设置-受信任的站点区域-对未标记为可安全执行脚本的ActiveX控件初始化并执形脚本(启用)

  2. hdu 3905(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3905 思路:dp[i][j]表示前i分钟,睡了j分钟收获的的最大价值,并记tmp_dp[i][j]为从 ...

  3. java对象和json数据转换实现方式3-使用jackson实现

    測试代码: package com.yanek.util.json; import java.io.IOException; import java.io.StringWriter; import j ...

  4. 编程之美 set 18 拈两堆石子游戏(3)

    题目 假设有两堆石头, 有两个玩家按照如下规则轮流取石头 每个人每次可以从两堆石头中取出数量相等的石头, 或者仅从一堆石头中取出任意数量的石头 最后把剩下的石头依次拿光的人取胜 首先取石头的人能否赢得 ...

  5. 编译OSG_FBX插件

    安装FBX的SDK,例如C:\Program Files\Autodesk\FBX\FBX SDK\2017.1\ 在CMake配置lib库路径的时候,使用 C:\Program Files\Auto ...

  6. 40 个顶级 jQuery 图片、内容滑块和幻灯片

    在这个快速发展的网络世界中,我们使用图片.内容滑块和幻灯片来给网站实现良好.有吸引力的外观.你可以吸引浏览者借助图像滑块让网站更加具有活力.使用 JavaScript 可以轻松实现轻量级的图片和内容滑 ...

  7. 谷歌浏览器chrome://inspect/#devices调试webview的页面和控制台布局错乱问题

    谷歌浏览器chrome://inspect/#devices调试webview的页面和控制台布局错乱问题 : 谷歌浏览器的版本过高,选择60版本即可: 版本 60.0.3080.5(正式版本)

  8. js 正则 exec() 和 match() 数据抽取

    js 的正则表达式平常用的不多,但以前抽取数据的时候用到过,主要是有这样的需求: var text='<td class="data">2014-4-4</td& ...

  9. Dart异步与消息循环机制

    Dart与消息循环机制 翻译自https://www.dartlang.org/articles/event-loop/ 异步任务在Dart中随处可见,例如许多库的方法调用都会返回Future对象来实 ...

  10. Servlet------>jsp自定义标签2(让标签体不显示)

    自定义标签能做什么: 1.移除java代码 2.控制jsp页面某一部分是否执行 3.控制整个jsp是否执行 3.jsp内容重复输出 4.修改jsp内容输出 2.控制jsp页面某一部分是否执行 tag1 ...