algorithm - STL C++

make_heap()

Declaration

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

Description

This function turns the elements in range from "xFirst" up through the one before "xLast" into a heap. The overloaded version uses the function "xComp" as a comparison function.

Header Include

#include <algorithm>

Overloads

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

Example

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

int main()
{
	using namespace std;

	// Create a vector instance
	vector<char> qV;
	qV.push_back('X');
	qV.push_back('o');
	qV.push_back('a');
	qV.push_back('X');
	qV.push_back('.');
	qV.push_back('n');
	qV.push_back('e');
	qV.push_back('t');

	vector<char>::iterator qIter;
	// Output the vector
	cout << "The Original Vector: ";
	for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
		cout << *qIter;
	}
	cout << endl;

	// Make the vector a heap
	make_heap(qV.begin(), qV.end());

	// Output the new vector
	cout << "The Vector as a Heap: ";
	for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
		cout << *qIter;
	}
	cout << endl;

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

Output

make_heap() Output
 

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