algorithm - STL C++

is_heap()

Declaration

template <class RandomAccessIterator>
bool is_heap(
	RandomAccessIterator xFirst,
	RandomAccessIterator xLast
);

Description

This function returns true is the range of entries starting at "xFirst" up to the one before "xLast" is 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>
bool is_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;
	for (int i = 0; i < 10; i += 2) {
		qV.push_back(20 -i);
	}

	vector<int>::iterator qIter;
	cout << "Vector: ";
	for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
		cout << *qIter << "  ";
	}
	cout << endl;
	// Check whether the vector is a heap
	bool bIsHeap = is_heap(qV.begin(), qV.end());
	cout << "The vector " << (bIsHeap ? "is" : "is not") << " a heap." << endl;

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

	cout << "Vector: ";
	for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
		cout << *qIter << "  ";
	}
	cout << endl;
	// Check whether the vector is a heap
	bIsHeap = is_heap(qV.begin(), qV.end());
	cout << "The vector " << (bIsHeap ? "is" : "is not") << " a heap." << endl;


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

Output

is_heap() Output
 

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