C++ STL algorithm, pop_heap() code 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++ pop_heap() to remove the largest element from the front of a heap to the next-to-last position in the range and then forms a new heap from the remaining elements in C++ programming

To show: How to use the C++ algorithm, pop_heap() to remove the largest element from the front of a heap to the next-to-last position in the range and then forms a new heap from the remaining elements in C++ programming

 

// C++ STL algorithm, pop_heap()

#include <vector>

#include <algorithm>

#include <functional>

#include <iostream>

using namespace std;

 

int main(void)

{

// container

vector <int> vec;

// iterator

vector <int>::iterator Iter1;

int i;

 

// push data in range

for(i = 1; i <= 9; i++)

vec.push_back(i);

 

// make vec vector a heap with default less-than ordering

cout<<"Operation: random_shuffle(vec.begin(), vec.end())"<<endl;

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

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

cout<<"The heaped version of vec vector data is: ";

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

cout<<*Iter1<<" ";

cout<<endl;

// add an element to the back of the heap

vec.push_back(11);

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

cout<<"The re-heaped vec vector data with 11 added: ";

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

cout<<*Iter1<<" ";

cout<<endl;

// remove the largest element from the heap

cout<<"\nOperation: pop_heap(vec.begin(), vec.end())"<<endl;

pop_heap(vec.begin(), vec.end());

cout<<"The heap vec vector data with 11 removed is: ";

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

cout<<*Iter1<<" ";

cout<<endl;

// make vec a heap with greater-than ordering with a 0 element

cout<<"\nOperation: make_heap(vec.begin(), vec.end(), greater<int>())"<<endl;

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

vec.push_back(0);

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

cout<<"The greater than re-heaped vec vector data puts the smallest element first:";

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

cout<<*Iter1<<" ";

cout<<endl;

// application of pop_heap() to remove the smallest element

cout<<"\nOperation: pop_heap(vec.begin(), vec.end(), greater<int>())"<<endl;

pop_heap(vec.begin(), vec.end(), greater<int>());

cout<<"The greater than heaped vec vector data with the smallest element removed from the heap is: ";

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

cout<<*Iter1<<" ";

cout<<endl;

return 0;

}

 

Output examples:

 

Operation: random_shuffle(vec.begin(), vec.end())

The heaped version of vec vector data is: 9 5 8 4 1 6 7 2 3

The re-heaped vec vector data with 11 added: 11 9 8 4 5 6 7 2 3 1

Operation: pop_heap(vec.begin(), vec.end())

The heap vec vector data with 11 removed is: 9 5 8 4 1 6 7 2 3 11

Operation: make_heap(vec.begin(), vec.end(), greater<int>())

The greater than re-heaped vec vector data puts the smallest element first:0 1 6 3 2 8 7 4 9 11 5

Operation: pop_heap(vec.begin(), vec.end(), greater<int>())

The greater than heaped vec vector data with the smallest element removed from the heap is: 1 2 6 3 5 8 7 4 9 11 0

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index