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 in [first,last) such a to that they form a heap.
AHeapis a by-organize the elements of a range that allows for fast retrieval of the element with the highest value at any m Oment (with Pop_heap), even repeatedly, while allowing for fast insertion of new elements (with Push_heap).
The element with the highest value was always pointed byfirst. The order of the other elements depends in the particular implementation, but it's consistent throughout all heap-related Functions of this header.
The Elements is compared using operator< (for the first version), or comp (forThe second): the element with the highest value was an element for which this would returnfalseWhen compared to every other element in the range.
The standard container adaptorpriority_queueCallsmake_heap,push_heapandpop_heapAutomatically to maintainHeap PropertiesFor a container.
Comp
Binary function that accepts the elements of 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 being less than the second in the Specific strict weak ordering it defines.
The function shall not modify any of its arguments.
This can either is a function pointer or a function object.
Make_heap (_first, _last, _comp)
The default is to build the maximum heap. For an int type, the smallest heap can be obtained by passing in the third argument to 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]<< "";
Output: 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;
Output 0.
Pop_heap (B.begin (), B.end ()); B.pop_back ();
cout<<b[9]<<endl;
As you can see, Pop_heap will place the top element of the heap at the end of the sequence, i.e. B.back ().
//Range Heap Example#include <iostream>//Std::cout#include <algorithm>//std::make_heap, std::p op_heap, std::p ush_heap, Std::sort_heap#include <vector>//std::vectorintMain () {intMyints[] = {Ten, -, -,5, the}; Std::vector<int> V (myints,myints+5); Std::make_heap (V.begin (), V.end ()); Std::cout<<"initial max heap:"<< V.front () <<'\ n'; std::p op_heap (V.begin (), V.end ()); V.pop_back (); Std::cout<<"Max heap after pop:"<< V.front () <<'\ n'; V.push_back ( About); std::p ush_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=0; I<v.size (); i++) Std::cout<<' '<<V[i]; Std::cout<<'\ n'; return 0;}
Output:
Initial Max heap:30
Max Heap after pop:20
Max Heap after push:99
Final sorted Range:5 10 15 20 99
Adding data to the heap
Push_heap (_first, _last)
to first add a number to the container, then call Push_heap ()
Delete data in the heap
Pop_heap (_first, _last)
To First call Pop_heap () and then delete the data in the container
Heap Sort
Sort_heap (_first, _last)
After sorting, it's no longer a valid heap.
Aboutspeaker the multi-merge sort of memory:
https://github.com/chenshuo/recipes/blob/master/algorithm/mergeN.cc
#include <algorithm>#include<iostream>#include<iterator>#include<vector>typedefintrecord;typedef std::vector<Record>File;structinput{Record value; size_t index; Constfile*file; ExplicitInput (Constfile*f): Value (-1), Index (0), file (f) {}BOOLNext () {if(Index < file->size ()) {Value= (*file) [index]; ++index; return true; } Else { return false; } } BOOL operator< (Constinput& RHS)Const { //Make_heap to build min-heap, for merging returnValue >Rhs.value; }}; File Mergen (Conststd::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::p op_heap (Inputs.begin (), Inputs.end ()); Output.push_back (Inputs.back (). value); if(Inputs.back (). Next ()) {std::p ush_heap (Inputs.begin (), Inputs.end ()); } Else{inputs.pop_back (); } } returnoutput;}intMain () {Const intKfiles = +; Std::vector<File>files (kfiles); for(inti =0; 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