algorithm - STL C++

upper_bound()

Declaration

template <class ForwardIterator, class X>
ForwardIterator upper_bound(
	ForwardIterator xFirst,
	ForwardIterator xLast,
	const Type& kxrTarget
);

Description

This function returns an iterator to the smallest element in range from "xFirst" up through the one before "xLast" that is greater than "kxrTarget." The overloaded version uses the function "xComp" as a comparison function.

Header Include

#include <algorithm>

Overloads

template <class ForwardIterator, class X, class BinaryPred>
ForwardIterator upper_bound(
	ForwardIterator xFirst,
	ForwardIterator xLast,
	const Type& kxrTarget,
	BinaryPred xComp
);

Example

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

int main()
{
	using namespace std;

	// Create a vector instance
	vector<int> qV;
	// Add the first eight even entries
	for (int i = 0; i < 16; i += 2) {
		qV.push_back(i);
	}

	vector<int>::iterator qIter;
	cout << "Ordered Vector: ";
	for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
		cout << *qIter << "  ";
	}
	cout << endl;

	// Find the least element that is greater than 9
	qIter = upper_bound(qV.begin(), qV.end(), 9);
	cout << *qIter << " is the upper bound of 9" << endl;

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

Output

upper_bound() Output
 

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