Heap in C++ STL | make_heap(), push_heap(), pop_heap(), sort_heap(), is_heap, is_heap_until() - GeeksforGeeks

17 Feb, 2021
Heap data structure can be implemented in a range using STL which allows faster input into heap and retrieval of a number always results in the largest number i.e. largest number of the remaining numbers is popped out each time. Other numbers of the heap are arranged depending upon the implementation.
Operations on heap :
1. make_heap() :- This function is used to convert a range in a container to a heap.
2. front() :- This function displays the first element of heap which is the maximum number.
// C++ code to demonstrate the working of // make_heap(), front()#include<bits/stdc++.h> using namespace std;int main(){          // Initializing a vector    vector<int> v1 = {20, 30, 40, 25, 15};          // Converting vector into a heap    // using make_heap()    make_heap(v1.begin(), v1.end());          // Displaying the maximum element of heap    // using front()    cout << "The maximum element of heap is : ";    cout << v1.front() << endl;          return 0;}
Output:
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
The maximum element of heap is : 40

 
3. push_heap() :- This function is used to insert elements into heap. The size of the heap is increased by 1. New element is placed appropriately in the heap.
4. pop_heap() :- This function is used to delete the maximum element of the heap. The size of heap is decreased by 1. The heap elements are reorganised accordingly after this operation.
// C++ code to demonstrate the working of // push_heap() and pop_heap()#include<bits/stdc++.h> using namespace std;int main(){          // Initializing a vector    vector<int> v1 = {20, 30, 40, 25, 15};          // Converting vector into a heap    // using make_heap()    make_heap(v1.begin(), v1.end());          // Displaying the maximum element of heap    // using front()    cout << "The maximum element of heap is : ";    cout << v1.front() << endl;          // using push_back() to enter element    // in vector    v1.push_back(50);          // using push_heap() to reorder elements    push_heap(v1.begin(), v1.end());          // Displaying the maximum element of heap    // using front()    cout << "The maximum element of heap after push is : ";    cout << v1.front() << endl;           // using pop_heap() to delete maximum element    pop_heap(v1.begin(), v1.end());    v1.pop_back();          // Displaying the maximum element of heap    // using front()    cout << "The maximum element of heap after pop is : ";    cout << v1.front() << endl;          return 0;}
Output:
The maximum element of heap is : 40
The maximum element of heap after push is : 50
The maximum element of heap after pop is : 40

5. sort_heap() :- This function is used to sort the heap. After this operation, the container is no longer a heap.
// C++ code to demonstrate the working of // sort_heap()#include<bits/stdc++.h> using namespace std;int main(){          // Initializing a vector    vector<int> v1 = {20, 30, 40, 25, 15};          // Converting vector into a heap    // using make_heap()    make_heap(v1.begin(), v1.end());          // Displaying heap elements     cout << "The heap elements are : ";    for (int &x : v1)        cout << x << " ";    cout << endl;          // sorting heap using sort_heap()    sort_heap(v1.begin(), v1.end());           // Displaying heap elements     cout << "The heap elements after sorting are : ";    for (int &x : v1)        cout << x << " ";          return 0;}
Output:
The heap elements are : 40 30 20 25 15 
The heap elements after sorting are : 15 20 25 30 40 

 
6. is_heap() :- This function is used to check whether the container is heap or not. Generally, in most implementations, the reverse sorted container is considered as heap. Returns true if container is heap else returns false.
6. is_heap_until() :- This function returns the iterator to the position till the container is the heap. Generally, in most implementations, the reverse sorted container is considered as heap.
// C++ code to demonstrate the working of // is_heap() and is_heap_until()#include<bits/stdc++.h> using namespace std;int main(){          // Initializing a vector    vector<int> v1 = {40, 30, 25, 35, 15};          // Declaring heap iterator    vector<int>::iterator it1;          // Checking if container is heap    // using is_heap()    is_heap(v1.begin(), v1.end())?    cout << "The container is heap ":    cout << "The container is not heap";    cout << endl;          // using is_heap_until() to check position     // till which container is heap    auto it = is_heap_until(v1.begin(), v1.end());        // Displaying heap range elements    cout << "The heap elements in container are : ";    for (it1=v1.begin(); it1!=it; it1++)       cout << *it1 << " ";         return 0;}
Output:
The container is not heap
The heap elements in container are : 40 30 25 

YouTubeGeeksforGeeks507K subscribersC++ Programming Language Tutorial | Heap in C++ STL | GeeksforGeeksWatch laterShareCopy link21/45InfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 3:25•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=BtZIpzxjQf4" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>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.
SamirKhan
CPP-Library
STL
C++
Heap
Heap
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Inheritance in C++
Map in C++ Standard Template Library (STL)
C++ Classes and Objects
Virtual Function in C++
Bitwise Operators in C/C++
Huffman Coding | Greedy Algo-3
K'th Smallest/Largest Element in Unsorted Array | Set 1
k largest(or smallest) elements in an array
Insertion and Deletion in Heaps
Building Heap from Array