**C++
STL algorithm, **
**push_heap() and **
**make_heap() program example**

**Compiler:
Visual C++ Express Edition 2005**

**Compiled
on Platform: Windows XP Pro SP2**

**Header
file: Standard**

**Additional
project setting: Set project to be compiled as C++**

**Project
-> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced
-> Compiled As: Compiled as C++ Code (/TP)**

**Other
info: none**

**To
do: Using the C++ push_heap() to add an element that is at the end of a range to
an existing heap consisting of the prior elements in the range and make_heap() to
convert elements from a specified range into a heap in which the first element is
the largest and for which a sorting criterion may be specified with a binary predicate**

**To
show: How to use the C++ algorithm, push_heap() and make_heap() to add an element
that is at the end of a range to an existing heap consisting of the prior elements
in the range and to convert elements from a specified range into a heap in which
the first element is the largest and for which a sorting criterion may be specified
with a binary predicate respectively**

// C++ STL algorithm, make_heap(), push_heap()

#include <vector>

#include <algorithm>

#include <functional>

#include <iostream>

using namespace std;

int main(void)

{

int i;

// vector container

vector <int> v1;

// iterator

vector <int>::iterator Iter1;

// push data in range

for (i = 10; i <= 20; i++)

v1.push_back(i);

// randomly shuffle the data

random_shuffle(v1.begin(), v1.end());

// print the data

cout<<"v1 vector with randomly shuffle data is: ";

for (Iter1 = v1.begin(); Iter1 != v1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl<<endl;

// make v1 a heap with default less-than ordering

make_heap(v1.begin(), v1.end());

cout<<"The default ordering heaped version of v1 vector is: ";

for (Iter1 = v1.begin(); Iter1 != v1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

// add an element to the heap

v1.push_back(9);

cout<<"The heap v1 vector with 9 pushed back is: ";

for (Iter1 = v1.begin(); Iter1 != v1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

push_heap(v1.begin(), v1.end());

cout<<"The reheaped v1 vector with 9 added is: ";

for (Iter1 = v1.begin(); Iter1 != v1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl<<endl;

// make v1 a heap with greater than ordering

make_heap(v1.begin(), v1.end(), greater<int>());

cout<<"The greater-than heaped version of v1 vector is: ";

for (Iter1 = v1.begin(); Iter1 != v1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

v1.push_back(7);

cout<<"The greater-than heap v1 vector with 7 pushed back is: ";

for (Iter1 = v1.begin(); Iter1 != v1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

push_heap(v1.begin(), v1.end(), greater<int>());

cout<<"The greater than reheaped v1 vector with 7 added is: ";

for (Iter1 = v1.begin(); Iter1 != v1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

return 0;

}

**Output
examples:**

v1 vector with randomly shuffle data is: 20 11 19 12 10 15 17 13 14 16 18

The default ordering heaped version of v1 vector is: 20 18 19 14 16 15 17 13 12 11 10

The heap v1 vector with 9 pushed back is: 20 18 19 14 16 15 17 13 12 11 10 9

The reheaped v1 vector with 9 added is: 20 18 19 14 16 15 17 13 12 11 10 9

The greater-than heaped version of v1 vector is: 9 10 15 12 11 19 17 13 14 18 16 20

The greater-than heap v1 vector with 7 pushed back is: 9 10 15 12 11 19 17 13 14 18 16 20 7

The greater than reheaped v1 vector with 7 added is: 7 10 9 12 11 15 17 13 14 18 16 20 19

Press any key to continue . . .