## equal_range
## Prototype
template <class ForwardIterator, class LessThanComparable> pair<ForwardIterator, ForwardIterator> equal_range(ForwardIterator first, ForwardIterator last, const LessThanComparable& value); template <class ForwardIterator, class T, class StrictWeakOrdering> pair<ForwardIterator, ForwardIterator> equal_range(ForwardIterator first, ForwardIterator last, const T& value, StrictWeakOrdering comp); ## Description
The first version of The second version of ## DefinitionDefined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h. ## Requirements on typesFor the first version: -
`ForwardIterator` is a model of ForwardIterator. -
`LessThanComparable` is a model of LessThanComparable. -
The ordering on objects of type
`LessThanComparable` is a*strict weak ordering*, as defined in the LessThanComparable requirements. -
`ForwardIterator` 's value type is the same type as`LessThanComparable` .
For the second version: -
`ForwardIterator` is a model of ForwardIterator. -
`StrictWeakOrdering` is a model of StrictWeakOrdering. -
`ForwardIterator` 's value type is the same type as`T` . -
`ForwardIterator` 's value type is convertible to`StrictWeakOrdering` 's argument type.
## PreconditionsFor the first version: -
`[first, last)` is a valid range. -
`[first, last)` is ordered in ascending order according to`operator<` . That is, for every pair of iterators`i` and`j` in`[first, last)` such that`i` precedes`j` ,`*j < *i` is`false` .
For the second version: -
`[first, last)` is a valid range. -
`[first, last)` is ordered in ascending order according to the functors`comp` . That is, for every pair of iterators`i` and`j` in`[first, last)` such that`i` precedes`j` ,`comp(*j, *i)` is`false` .
## ComplexityThe number of comparisons is logarithmic: at most ## Exampleint main() { int A[] = { 1, 2, 3, 3, 3, 5, 8 }; const int N = sizeof(A) / sizeof(int); for (int i = 2; i <= 4; ++i) { pair<int*, int*> result = equal_range(A, A + N, i); cout << endl; cout << "Searching for " << i << endl; cout << " First position where " << i << " could be inserted: " << result.first - A << endl; cout << " Last position where " << i << " could be inserted: " << result.second - A << endl; if (result.first < A + N) cout << " *result.first = " << *result.first << endl; if (result.second < A + N) cout << " *result.second = " << *result.second << endl; } } The output is: Searching for 2 First position where 2 could be inserted: 1 Last position where 2 could be inserted: 2 *result.first = 2 *result.second = 3 Searching for 3 First position where 3 could be inserted: 2 Last position where 3 could be inserted: 5 *result.first = 3 *result.second = 5 Searching for 4 First position where 4 could be inserted: 5 Last position where 4 could be inserted: 5 *result.first = 5 *result.second = 5 ## Notes[1] Note that you may use an ordering that is a strict weak ordering but not a total ordering; that is, there might be values [2] Note that [3] This difference between RandomAccessIterator and ForwardIterator is simply because ## See also |