STL Heap Usage

Source: Internet
Author: User

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 returnfalseWhen 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.