The following program can demonstrates you how to implement the make_heap() function of the C++ language in the C++ programming as follows :
Code:
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
int main( )
{
using namespace std;
vector <int> w1, w2;
vector <int>::iterator It1, It2;
int i;
for ( i = 0 ; i <= 9 ; i++ )
{
w1.push_back( i );
}
random_shuffle( w1.begin( ), w1.end( ) );
cout << "Vector w1 is ( " ;
for ( It1 = w1.begin( ) ; It1 != w1.end( ) ; It1++ )
cout << *It1 << " ";
cout << ")." << endl;
make_heap ( w1.begin( ), w1.end( ) );
cout << "The heaped version of vector w1 is ( " ;
for ( It1 = w1.begin( ) ; It1 != w1.end( ) ; It1++ )
cout << *It1 << " ";
cout << ")." << endl;
make_heap ( w1.begin( ), w1.end( ), greater<int>( ) );
cout << "The greater-than heaped version of w1 is ( " ;
for ( It1 = w1.begin( ) ; It1 != w1.end( ) ; It1++ )
cout << *It1 << " ";
cout << ")." << endl;
}
Output
Code:
Vector w1 is ( 8 1 9 2 0 5 7 3 4 6 ).
The heaped version of vector w1 is ( 9 6 8 4 1 5 7 3 2 0 ).
The greater-than heaped version of w1 is ( 0 1 5 2 6 8 7 3 4 9 ).
Bookmarks