std::equal_range
|
default (1)
|
template
pair
equal_range (ForwardIterator first, ForwardIterator last, const T& val);
|
|
custom (2)
|
template
pair
equal_range (ForwardIterator first, ForwardIterator last, const T& val,
Compare comp);
|
Get subrange of equal elements
Returns the bounds of the subrange that includes all the elements of the range [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 bare considered equivalent if (!(a
The elements in the range shall already be according to this same criterion (operator< or comp), or at least 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:
1
2
3
4
5
6
7
|
template
pair
equal_range (ForwardIterator first, ForwardIterator last, const T& val)
{
ForwardIterator it = std::lower_bound (first,last,val); return std::make_pair ( it, std::upper_bound(it,last,val) );
}
|
Parameters
first, last to the initial and final positions of a (or properly ) sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed byfirst 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 of operator<.
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 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 and respectively.
Example
// equal_range example
// equal_range example
#include // std::cout
#include // std::equal_range, std::sort
#include // std::vector
bool mygreater (int i,int j) { return (i>j); }
int main () {
int myints[] = {10,20,30,30,20,10,10,20};
std::vector v(myints,myints+8); // 10 20 30 30 20 10 10 20
std::pair::iterator,std::vector::iterator> bounds;
// using default comparison:
std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30
bounds=std::equal_range (v.begin(), v.end(), 20); // ^ ^
// using "mygreater" as comp:
std::sort (v.begin(), v.end(), mygreater); // 30 30 20 20 20 10 10 10
bounds=std::equal_range (v.begin(), v.end(), 20, mygreater); // ^ ^
std::cout << "bounds at positions " << (bounds.first - v.begin());
std::cout << " and " << (bounds.second - v.begin()) << '\n';
return 0;
}
Output:
bounds at positions 2 and 5
|
Complexity
On average, up to twice logarithmic in the between first and last: Performs approximately 2*log
2(N)+1element comparisons (where N is this distance).
On
non- iterators, the iterator 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
Return iterator to lower bound
(function template )
Return iterator to upper bound
(function template )
Test if value exists in sorted sequence
(function template )
阅读(1318) | 评论(0) | 转发(0) |