algorithm - STL C++

sort_heap()

Declaration

template <class RandomIter>
void sort_heap(
	RandomIter xFirst,
	RandomIter xLast
);

Description

This function sorts the entries in the range starting from "xFirst" up to the entry before "xLast" if they already in the order of a heap. The overloaded version takes a comparison function, "xComp," and uses that to compare entries instead of less than.

Header Include

#include <algorithm>

Overloads

template <class RandomIter, class Pred>
void sort_heap(
	RandomIter xFirst,
	RandomIter xLast,
	Pred xComp
);

Example

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

int main()
{
	using namespace std;

	// Create a vector with random entries
	vector<int> qV;
	for (int i = 0; i < 10; ++i) {
		qV.push_back(rand() % 20);
	}

	// Output the original randomized vector
	vector<int>::iterator qIter;
	cout << "Random Vector: ";
	for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
		cout << *qIter << "  ";
	}
	cout << endl;

	// Sort the entries into heap order
	make_heap(qV.begin(), qV.end());

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

	// Sort the heap vector
	sort_heap(qV.begin(), qV.end());

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

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

Output

sort_heap() Output
 

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