Sometimes in order to maintain the data, we use the STL heap to maintain a sequence.
First you need to figure out the difference between heap and stack, that is, heap and stack
The heap in the STL is the largest heap by default.
First introduce the Push_heap,pop_heap,make_heap,sort_heap four algorithms, these four are not c++11 new additions.
The first is how to generate a maximum push:
Make_heap
Prototype:
template <class RandomAccessIterator> void make_heap (RandomAccessIterator first, RandomAccessIterator last); template <classclass Compare> void make_heap (RandomAccessIterator first, RandomAccessIterator last, Compare comp );
The first one is the default, maximum heap.
Role:
Rearranges the elements in the range [First,last] in such a to that they form a heap.
Push_heap,pop_heap
These two will not be mentioned again, in the code has appeared.
Sort_heap
Sort Elements of Heap
Take a look at the above four methods of application:
#include <iostream> //std::cout #include <algorithm> //STD::MAKE_HEAP, std::p op_heap, std::p ush_heap, Std::sort_heap #include <vector> //std::vector intMain () {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(unsignedi =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
Next look at the above based on the new additions to c++11:
Is_heap
Role:
Returns true if the range [first,last) forms a heap, as if constructed with Make_heap.
Application:
#include <iostream> //std::cout #include <algorithm> //Std::is_heap, STD::MAKE_HEAP, std::p op_heap #include <vector> //std::vector intMain () {STD:: vector<int>Foo {9,5,2,6,4,1,3,8,7};if(!STD:: Is_heap (Foo.begin (), Foo.end ()))STD:: Make_heap (Foo.begin (), Foo.end ());STD::cout<<"Popping out elements:"; while(!foo.empty ()) {STD::p op_heap (Foo.begin (), Foo.end ());//Moves largest element to back STD::cout<<"'<< Foo.back ();//Prints backFoo.pop_back ();//POPs element out of container}STD::cout<<' \ n ';return 0;}
Is_heap_until
Role:
Find first element not in heap order
Returns an iterator to the first element in the range [First,last] which is not in a valid position if the range is consid Ered a heap (as if constructed with Make_heap).
Application:
#include <iostream> //std::cout #include <algorithm> //Std::is_heap_until, Std::sort, Std::reverse #include <vector> //std::vector intMain () {STD:: vector<int>Foo {2,6,9,3,8,4,5,1,7};STD:: Sort (Foo.begin (), Foo.end ());STD:: Reverse (Foo.begin (), Foo.end ());AutoLast =STD:: Is_heap_until (Foo.begin (), Foo.end ());STD::cout<<"the"<< (Last-foo.begin ()) <<"First elements is a valid heap:"; for(AutoIt=foo.begin (); It!=last; ++it)STD::cout<<"'<< *it;STD::cout<<' \ n ';return 0;}
Note: Examples are from www.cplusplus.com website
C++11 new Feature application--introduction of several new convenience algorithms (heap usage in STL, Max Heap)