make_heap() in C++ STL

13 Jun, 2020
make_heap() is used to transform a sequence into a heap. A heap is a data structure which points to highest( or lowest) element and making its access in O(1) time. Order of all the other elements depends upon particular implementation, but remains consistent throughout. This function is defined in the header “algorithm“. There are two implementations of make_heap() function. Both of them are explained through this article.Syntax 1 : make_heap(iter_first, iter_last)
Template :void make_heap (RandomAccessIterator first, RandomAccessIterator last);Parameters :first : The pointer to the starting element of sequence that has tobe transformed into heap.last : The pointer to the next address to last element of sequence thathas to be transformed into heap.
Below is the demonstrating code :
// C++ code to demonstrate the working of // make_heap() using syntax 1  #include<iostream>#include<algorithm> // for heap #include<vector>using namespace std;  int main(){    // initializing vector;    vector<int> vi = { 4, 6, 7, 9, 11, 4 };          // using make_heap() to transform vector into    // a max heap    make_heap(vi.begin(),vi.end());          //checking if heap using     // front() function    cout << "The maximum element of heap is : ";    cout << vi.front() << endl;      }
Output:
The maximum element of heap is : 11

Syntax 2 : make_heap(iter_first, iter_last, comp)
Template :void make_heap (RandomAccessIterator first, RandomAccessIterator last, comp);Parameters :first : The pointer to the starting element of sequence that has to be transformed into heap.last : The pointer to the next address to last element of sequence that has to be transformed into heap.comp : The comparator function that returns a boolean true/false of the each elements compared. This function accepts two arguments. This can be function pointer or function object and cannot change values.
Below is the demonstrating code :
// C++ code to demonstrate the working of // make_heap() using syntax 2   #include<iostream> #include<algorithm> // for heap #include<vector> using namespace std;   // comparator function to make min heap struct greaters{ bool operator()(const long& a,const long& b) const{     return a>b; } };   int main() {     // initializing vector;     vector<int> vi = { 15, 6, 7, 9, 11, 45 };           // using make_heap() to transform vector into     // a min heap     make_heap(vi.begin(),vi.end(), greaters());           // checking if heap using     // front() function     cout << "The minimum element of heap is : ";     cout << vi.front() << endl;       } 
Output:
The minimum element of heap is : 6

Possible application : This function can be used in scheduling. In scheduling, a new element is inserted dynamically in iterations. Sorting again and again to get maximum takes much complexity O(nlogn), instead of that we use “push_heap()” function to heapify the heap made in O(logn) time . The code below depicts its implementation.
// C++ code to demonstrate  // application of make_heap() (max_heap)// priority scheduling  #include<iostream>#include<algorithm> // for heap #include<vector>using namespace std;  int main(){    // initializing vector;    // initial job priorities    vector<int> vi = { 15, 6, 7, 9, 11, 19};          // No. of incoming jobs.    int k = 3;          // using make_heap() to transform vector into    // a min heap    make_heap(vi.begin(),vi.end());          // initializing job variable    int a = 10;          for ( int i=0; i<k; i++)    {          // push a job with priority level    vi.push_back(a);           // transform into heap ( using push_heap() )    push_heap(vi.begin(), vi.end());          //checking top priority job    // front() function    cout << "Job with maximum priority is : ";    cout << vi.front() << endl;          // increasing job priority level    a = a + 10;          }      }
Output:
Job with maximum priority is : 19
Job with maximum priority is : 20
Job with maximum priority is : 30

This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
shubham_singh
BhavitJain
STL
C++
Heap
Heap
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sorting a vector in C++
Polymorphism in C++
std::string class in C++
Friend class and function in C++
Pair in C++ Standard Template Library (STL)
HeapSort
K'th Smallest/Largest Element in Unsorted Array | Set 1
Binary Heap
Introduction to Data Structures
Huffman Coding | Greedy Algo-3