algorithm - STL C++

pop_heap()

Declaration

template <class RandomAccessIterator>
void pop_heap(
	RandomAccessIterator xFirst,
	RandomAccessIterator xLast
);

Description

This function pops the largest element from the elements organized into a heap in the range "xFirst" up through the one before "xLast" and places the largest element at the end of the range. The elements before the largest one are reorganized to form a smaller heap. The overloaded version uses the function "xComp" as a less than comparison function.

Header Include

#include <algorithm>

Overloads

template <class RandomAccessIterator, class BinaryPred>
void pop_heap(
	RandomAccessIterator xFirst,
	RandomAccessIterator xLast,
	BinaryPred xComp
);

Example

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
	using namespace std;

	// Create a vector instance
	vector<int> qV;
	qV.push_back(45);
	qV.push_back(83);
	qV.push_back(3);
	qV.push_back(49);
	qV.push_back(97);
	qV.push_back(16);
	qV.push_back(34);
	qV.push_back(51);

	vector<int>::iterator qIter;
	// Make the vector a heap
	make_heap(qV.begin(), qV.end());

	// Output the heap
	cout << "Output the Heap : ";
	for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
		cout << *qIter << "  ";
	}
	cout << endl;

	// Pop the largest int (97) and put it last
	pop_heap(qV.begin(), qV.end());

	// Output the new vector
	cout << "Output after Pop: ";
	for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
		cout << *qIter << "  ";
	}
	cout << endl;

	// Keep the window open
	cin.get();
	return 0;
}

Output

pop_heap() Output
 

© 2007–2024 XoaX.net LLC. All rights reserved.