algorithm - STL C++

lower_bound()

Declaration

template <class ForwardIterator, class X>
ForwardIterator lower_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 less than or equal to "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 lower_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 less than or equal
	qIter = lower_bound(qV.begin(), qV.end(), 9);
	cout << "9 is the lower bound of " << *qIter << endl;

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

Output

lower_bound() Output
 

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