algorithm - STL C++

equal_range()

Declaration

template <class ForwardIterator, class X>
pair<ForwardIterator, ForwardIterator> equal_range(
	ForwardIterator xFirst,
	ForwardIterator xLast,
	const X& kxrValue
);

Description

This function checks the ordered entries that are in the range from "xFirst" up to the entry before "xLast" to find all entries equal to "kxrValue" and output a pair of iterators to make where the set of entries begins and ends.

Header Include

#include <algorithm>

Overloads

template <class ForwardIterator, class X, class Predicate>
pair<ForwardIterator, ForwardIterator> equal_range(
	ForwardIterator xFirst,
	ForwardIterator xLast,
	const X& kxrValue,
	Predicate xTest
);

Example

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

int main()
{
	using namespace std;

	// Create an ordered vector instance
	vector<int> qV;
	qV.push_back(2);
	qV.push_back(4);
	qV.push_back(5);
	qV.push_back(5);
	qV.push_back(5);
	qV.push_back(7);
	qV.push_back(9);

	// Calculate the range
	pair<vector<int>::iterator, vector<int>::iterator> qRange
        = equal_range(qV.begin(), qV.end(), 5);

	// Output the entries in the range
	vector<int>::iterator qIter;
	cout << "Inside Range: ";
	for (qIter = qRange.first; qIter !=  qRange.second; ++qIter) {
		cout << *qIter << " ";
	}
	cout << endl;
	// Output the range bounds inside the vector
	cout << "Vector: ";
	for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
		if (qIter == qRange.first || qIter == qRange.second) {
			cout << "| ";
		}
		cout << *qIter << " ";
	}
	cout << endl;


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

Output

equal_range() Output
 

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