The C++ Standard Library consists of the header, <algorithm>
which defines a collection of functions (principally designed to deal with a range of elements). push_heap()
is a method in the STL that is used to insert an element into the heap. The size of the heap effectively increases by one.
Syntax:
push_heap( iterator_begin, iterator_end, comparator ); //comparator is optional
Parameters: The push_heap()
method accepts 3 parameters
- iterator_begin: Iterator to the initial position of new heap
- iterator_end: Iterator to the final position of new heap ( Note: the range is [iterator_begin,iterator_end) )
- comparator: Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool.
Return value: none
Example of push_heap() method
#include<iostream> #include<algorithm> #include<vector> using namespace std; int main() { vector<int> v; v.push_back(4); v.push_back(3); v.push_back(8); v.push_back(-5); v.push_back(7); make_heap(v.begin(), v.end()); cout<<"Initial heap: "<<v.front()<<endl; //max val v.push_back(12); push_heap(v.begin(), v.end()); cout<<"New heap: "<<v.front()<<endl; //max val }
Output:
Initial heap: 8 New heap: 12