algorithm - STL C++

is_heap_until()

Declaration

template <class RandomAccessIterator>
RandomAccessIterator is_heap_until(
	RandomAccessIterator xFirst,
	RandomAccessIterator xLast
);

Description

This function returns an iterator that points to the first item in the range of entries starting at "xFirst" up to the one before "xLast" is not a heap. In the overloaded version, the comparison function "xComp" is used to define the ordering.

Header Include

#include <algorithm>

Overloads

template <class RandomAccessIterator, class BinaryPred>
RandomAccessIterator is_heap_until(
	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;
	for (int i = 0; i < 10; i += 2) {
		qV.push_back(20 -i);
	}

	vector<int>::iterator qEndOfHeap;
	vector<int>::iterator qIter;
	cout << "Vector: ";
	for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
		cout << *qIter << "  ";
	}
	cout << endl;
	// Find the end of the heap
	qEndOfHeap = is_heap_until(qV.begin(), qV.end());

	cout << "Heap: ";
	for (qIter = qV.begin(); qIter != qEndOfHeap; ++qIter) {
		cout << *qIter << "  ";
	}
	cout << endl << "After Heap: ";
	for (qIter = qEndOfHeap; qIter != qV.end(); ++qIter) {
		cout << *qIter << "  ";
	}
	cout << endl << endl;

	// Reverse the vector
	reverse(qV.begin(), qV.end());

	cout << "Vector: ";
	for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
		cout << *qIter << "  ";
	}
	cout << endl;
	// Find the end of the heap
	qEndOfHeap = is_heap_until(qV.begin(), qV.end());

	cout << "Heap: ";
	for (qIter = qV.begin(); qIter != qEndOfHeap; ++qIter) {
		cout << *qIter << "  ";
	}
	cout << endl << "After Heap: ";
	for (qIter = qEndOfHeap; qIter != qV.end(); ++qIter) {
		cout << *qIter << "  ";
	}
	cout << endl;

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

Output

is_heap_until() Output
 

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