Brief introduction
The heap has find time complexity O (1), find, insert, delete time complexity O (logn) characteristics, the STL heap related operations are as follows:
Make_heap ()
Push_heap ()
Pop_heap ()
Sort_heap ()
Reverse ()
This focus is on the make_heap (), according to its creation of the heap has a size heap of points. Its function prototype is as follows:
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); |
The function is interpreted as follows:
Make heap from range
Rearranges the elements in the range[first,last)
In such a to that they form aHeap.
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 usingoperator<
(for the first version), orcomp(For the second): the element with the highest value was an element for which this would returnfalse
When compared to every other element in the range.
The standard container adaptorpriority_queueCallsmake_heap,push_heapandpop_heapAutomatically to maintainHeap PropertiesFor a container.
Parameters
-
-
First, last
-
-
Random-access iterators to the initial and final positions of the sequence to being transformed into a heap. The range used
[first,last)
is, which contains all the elements between first last and, including the element pointed by But isn't the first element pointed by last .
RandomAccessIteratorshall point to a type for which are swap properly defined and which is both
move-constructible and move-assignable.
-
-
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.
-
-
-
basically can be summed up as the default parameter prototype to create the largest heap, with the comp function parameters of the prototype can be based on the selection of comp to create the required heap, according to the following instances can be, the default is less than the number of the comparison, create the largest heap, if comp's comparison operation is ">" to create the minimum heap.
-
Instance
1. Create maximum heap (default function)
//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 5 15 about
Can get the maximum heap.
2. Minimum heap creation (using the custom (2) function)
#include <iostream>#include<vector>#include<algorithm>structDoc {Doublerank; ExplicitDocDoubler): Rank (r) {}};structDoc_rank_greater_than {BOOL operator() (DocConst& A, docConst& B)Const { returnA.rank >B.rank; }};intMain () {std::vector<doc>Docvec; Docvec.push_back (Doc (4) ); Docvec.push_back (Doc (3) ); Docvec.push_back (Doc (2) ); Docvec.push_back (Doc (1) ); Std::make_heap (Docvec.begin (), Docvec.end (), Doc_rank_greater_than ()); Std::cout<< Docvec.front (). Rank <<'\ n';}
Output:
1
Can get the minimum heap.
Summary Custom functions can create the corresponding maximum minimum heap based on the objects in the container.
STL Heap Usage