#include <iostream>
using std::cout;
using std::endl;
#include <algorithm>
#include <vector>
#include <iterator>
int main()
{
int a[ 10 ] = { 3, 10, 2, 7, 4, 8, 1, 9, 5, 6 };
std::vector< int > v( a, a + 10 ); // copy of a
std::vector< int > v2;
std::ostream_iterator< int > output( cout, " " );
for ( int i = 0; i < 10; i++ )
{
v2.push_back( a[ i ] );
std::push_heap( v2.begin(), v2.end() );
cout << "\nv2 after push_heap(a[" << i << "]): ";
std::copy( v2.begin(), v2.end(), output );
}
for ( int j = 0; j < v2.size(); j++ )
{
cout << "\nv2 after " << v2[ 0 ] << " popped from heap\n";
std::pop_heap( v2.begin(), v2.end() - j );
std::copy( v2.begin(), v2.end(), output );
}
cout << endl;
return 0;
}
|