\\
Adds an element to a heap
Key Facts
Gyroscopic Couple: The rate of change of angular momentum () = (In the limit).- = Moment of Inertia.
- = Angular velocity
- = Angular velocity of precession.
Blaise Pascal (1623-1662) was a French mathematician, physicist, inventor, writer and Catholic philosopher.
Leonhard Euler (1707-1783) was a pioneering Swiss mathematician and physicist.
Definition
The push_heap() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.Interface
#include <algorithm> template < class RandomAccessIterator > void push_heap( RandomAccessIterator first, RandomAccessIterator last ); template < class RandomAccessIterator, class BinaryPredicate > void push_heap( RandomAccessIterator first, RandomAccessIterator last, BinaryPredicate comp );Parameters:
Parameter | Description |
---|---|
first | A random-access iterator addressing the position of the first element in the heap |
last | A random-access iterator addressing the position one past the final element in the range to be converted into a heap |
comp | User-defined predicate function object that defines sense in which one element is less than another. A binary predicate takes two arguments and returns true when satisfied and false when not satisfied |
Description
Push_heap inserts the element at the positionlast - 1
into the heap defined by the range [first, last-1)
.
The first version compares objects using operator<
and the second compares objects using a function object comp
.Example:
Example - push_heap algorithm
Problem
This program illustrates the use of the STL push_heap() algorithm (default
version) to add a value to a (maximum) heap of integers.
Workings
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int a[] = {36, 19, 25, 17, 3, 7, 1, 2,}; vector<int> v(a, a+8); cout <<"\nHere are the values in the vector (the heap):\n"; for (vector<int>::size_type i=0; i<v.size(); i++) cout <<v.at(i)<<" "; cout <<"\nNow we add the value 100 to the heap."; v.push_back(100); push_heap(v.begin(), v.end()); cout <<"\nHere are the revised contents of the vector (the new heap):\n"; for (vector<int>::size_type i=0; i<v.size(); i++) cout <<v.at(i)<<" "; return 0; }
Solution
Output:
Here are the values in the vector (the heap):
36 19 25 17 3 7 1 2 Now we add the value 100 to the heap. Here are the revised contents of the vector (the new heap):
100 36 25 19 3 7 1 2 17
36 19 25 17 3 7 1 2 Now we add the value 100 to the heap. Here are the revised contents of the vector (the new heap):
100 36 25 19 3 7 1 2 17
References