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"));
}

最新文章

  1. jquery.mobile手机网页简要
  2. MySQL学习笔记(2/2)
  3. 麦克斯韦方程组 (Maxwell&#39;s equation)的简单解释
  4. 100735G
  5. App主界面Tab实现方法
  6. Unity中Collider和刚体Collider性能对比
  7. 点击弹出固定大小的新窗口(js实现)
  8. 如何改变Activity在当前任务堆栈中的顺序,Intent参数大全
  9. SqlSever基础 union 联合查询,厉害的并集 重复项只显示一个 两个查询结果并在一起后排序
  10. SGU 156. Strange Graph(欧拉路)
  11. poj 2773 利用欧拉函数求互质数
  12. watch命令详解(linux)
  13. ZZCMS8.1|代码审计
  14. CF1101G (Zero XOR Subset)-less
  15. Django认证系统auth认证
  16. [转]Eureka自我保护机制、健康检查的作用、actuator模块监控
  17. VS Code打开使用IDEA搭建的Spring Boot项目运行提示&quot;snakeyaml was not found on the classpath&quot;错误
  18. SQL SERVER 游标循环读取表数据
  19. SkylineGlobe API 如何以图层的方式导入MPT地形
  20. C#网络编程之编码解码

热门文章

  1. Hadoop源码分析之读文件时NameNode和DataNode的处理过程
  2. RAII in C++
  3. hrbustoj 1306:再遇攻击(计算几何,判断点是否在多边形内,水题)
  4. 【Python】python-memory-management
  5. 怎么隐藏MathType标尺
  6. 【转】Native Thread for Win32 B-Threads Synchronization(通俗易懂,非常好)
  7. 深入理解JS之Scope链
  8. 修改tomcat服务器默认端口号
  9. Transact-SQL小知识
  10. 非IE图片上传预览