Heap C + + operation large top heap, small top heap

Source: Internet
Author: User

In C + +, although the heap is not like a vector, set and the like have implemented data structure, but in algorithm.h implemented some related template functions. Here are some sample applications

http://www.cplusplus.com/reference/algorithm/pop_heap/

#include <iostream>#include<algorithm>//make_heap (), Pop_heap (), Push_heap ( )#include <vector>using namespacestd;voidPrintvector (vector<int> &num) {     for(inti =0; I < num.size (); i++) cout<<num[i]<<" "; cout<<Endl;}intMain () {//Init    intArr[] = {5,1,6,9,4,3}; Vector<int> num (arr,arr+6);    Printvector (num); //Buildmake_heap (Num.begin (), Num.end ()); Printvector (num); //9 5 6 1 4 3 default large Top heap//get the biggest number//from the vector angle.cout<<num[0]<<endl;//9//or Num.front ();Cout<<num.front () <<endl;//9//Delete Heap Top, which is the largest element//The return value is void//Place the top element of the heap on the last position//after an element is ejected, the remaining heap is rebuilt and the heap remains in the naturepop_heap (Num.begin (), Num.end ()); Printvector (num); //6 5 3 1 4 9//vector Delete End elementNum.pop_back ();    Printvector (num); Num.push_back (7);//First, expand on the vector and add an element to the tail .Printvector (num);//6 5 3 1 4 7Push_heap (Num.begin (), Num.end ());//the last element of the specified interval is added to the heap and the entire interval becomes a new heap. Note that all elements except the last element already constitute a heap. Printvector (num);//7 5 6 1 4 3//determine if the heap    BOOLRET =is_heap (Num.begin (), Num.end ()); cout<<ret<<Endl; Num.push_back (9);  Printvector (num); //7 5 6 1 4 3 9cout<< is_heap (Num.begin (), Num.end ()) <<Endl;    Push_heap (Num.begin (), Num.end ()); Printvector (num); //9 5 7 1 4 3 6sort_heap (Num.begin (), Num.end ()); Printvector (num); //1 3 4 5 6 7 9} //Small Top Pile#include <iostream>#include<vector>#include<algorithm>using namespacestd;classgreater_class{ Public:    BOOL operator()(intAintb) {returnA >b; }};intMain () {//Init    intArr[] = {5,1,6,9,4,3}; Vector<int> num (arr,arr+6);    Printvector (num);    Make_heap (Num.begin (), Num.end (), Greater_class ()); Printvector (num); //1 4 3 9 5 6Num.push_back (2); Printvector (num); //1 4 3 9 5 6 2push_heap (Num.begin (), Num.end (), Greater_class ()); Printvector (num); //1 4 2 9 5 6 3     while(Num.size ()) {Pop_heap (Num.begin (), Num.end (), Greater_class ()); LongMin =Num.back ();          Num.pop_back (); cout<< min <<Std::endl; } //1 2 3 4 5 6 9}

  

Related Article

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.