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 ); |
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[] = {10,20,30,5,15}; std::vector<int> v(myints,myints+5); std::make_heap (v.begin(),v.end()); std::cout << "initial max heap : " << v.front() << ' '; std::pop_heap (v.begin(),v.end()); v.pop_back(); std::cout << "max heap after pop : " << v.front() << ' '; v.push_back(99); std::push_heap (v.begin(),v.end()); std::cout << "max heap after push: " << v.front() << ' '; std::sort_heap (v.begin(),v.end()); std::cout << "final sorted range :"; for (unsigned i=0; i<v.size(); i++) std::cout << ' ' << v[i]; std::cout << ' '; return 0; }
输出:
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(-1), index(0), 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 = 0; 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 = 32; std::vector<File> files(kFiles); for (int i = 0; i < kFiles; ++i) { File file(rand() % 1000); 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, " ")); }