function template
<algorithm>
std::equal_range
| default (1) | template <class ForwardIterator, class T> pair<ForwardIterator,ForwardIterator> equal_range (ForwardIterator first, ForwardIterator last, const T& val); |
|---|---|
| custom (2) | template <class ForwardIterator, class T, class Compare> pair<ForwardIterator,ForwardIterator> equal_range (ForwardIterator first, ForwardIterator last, const T& val, Compare comp); |
Get subrange of equal elements
[first,last) with values equivalent to val.
The elements are compared using operator< for the first version, and comp for the second. Two elements, a and b are considered equivalent if (!(a<b) && !(b<a)) or if (!comp(a,b) && !comp(b,a)).
The elements in the range shall already be sorted according to this same criterion (operator< or comp), or at least partitioned with respect to val.
If val is not equivalent to any value in the range, the subrange returned has a length of zero, with both iterators pointing to the nearest value greater than val, if any, or to last, if val compares greater than all the elements in the range.
The behavior of this function template is equivalent to:
|
|
Parameters
- first, last
- Forward iterators to the initial and final positions of a sorted (or properly partitioned) sequence. The range used is
[first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last. - val
- Value of the subrange to search for in the range.
For (1), T shall be a type supporting being compared with elements of the range[first,last)as either operand ofoperator<. - comp
- Binary function that accepts two arguments of the type pointed by ForwardIterator (and of type T), and returns a value convertible to
bool. The value returned indicates whether the first argument is considered to go before the second.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.
Return value
A pair object, whose member pair::first is an iterator to the lower bound of the subrange of equivalent values, and pair::second its upper bound.The values are the same as those that would be returned by functions lower_bound and upper_bound respectively.
Example
|
|
Output:
bounds at positions 2 and 5
Complexity
On average, up to twice logarithmic in the distance between first and last: Performs approximately2*log2(N)+1 element comparisons (where N is this distance).On non-random-access iterators, the iterator advances produce themselves an additional up to twice linear complexity in N on average.
Data races
The objects in the range[first,last) are accessed.Exceptions
Throws if either an element comparison or an operation on an iterator throws.Note that invalid arguments cause undefined behavior.
See also
- lower_bound
- Return iterator to lower bound (function template)
- upper_bound
- Return iterator to upper bound (function template)
- binary_search
- Test if value exists in sorted sequence (function template)