std::lower_bound
<algorithm>
template <class ForwardIterator, class T> ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last, const T& value ); template <class ForwardIterator, class T, class Compare> ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last, const T& value, Compare comp );
Return iterator to lower bound
Returns an iterator pointing to the first element in the sorted range
[first,last)
which does not compare less than
value
. The comparison is done using either
operator<
for the first version, or
comp
for the second.
For the function to yield the expected result, the elements in the range shall already be ordered according to the same criterion ( operator< or comp ).
Unlike upper_bound , this function returns an iterator to the element also if it compares equivalent to value and not only if it compares greater.
The behavior of this function template is equivalent to:
| |
std::upper_bound
<algorithm>
template <class ForwardIterator, class T> ForwardIterator upper_bound ( ForwardIterator first, ForwardIterator last, const T& value ); template <class ForwardIterator, class T, class Compare> ForwardIterator upper_bound ( ForwardIterator first, ForwardIterator last, const T& value, Compare comp );
Return iterator to upper bound
Returns an iterator pointing to the first element in the sorted range
[first,last)
which compares greater than
value
. The comparison is done using either
operator<
for the first version, or
comp
for the second.
For the function to yield the expected result, the elements in the range shall already be ordered according to the same criterion ( operator< or comp ).
Unlike lower_bound , this function does not return an iterator to the element if it compares equivalent to value , but only if it compares strictly greater.
The behavior of this function template is equivalent to:
| |
std::equal_range
<algorithm>
template <class ForwardIterator, class T> pair<ForwardIterator,ForwardIterator> equal_range ( ForwardIterator first, ForwardIterator last, const T& value ); template <class ForwardIterator, class T, class Compare> pair<ForwardIterator,ForwardIterator> equal_range ( ForwardIterator first, ForwardIterator last, const T& value, Compare comp );
Get subrange of equal elements
Returns the bounds of the largest subrange that includes all the elements of the
[first,last)
with values equivalent to
value
.
The comparison is performed using either operator< for the first version, or comp for the second: A value, a , is considered equivalent to another, b , when (!(a<b) && !(b<a)) or (!comp(a,b) && !comp(b,a))
If value 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 value , if any, or to last , if value compares greater than all the elements in the range.
For the function to yield the expected result, the elements in the range shall already be ordered according to the same criterion ( operator< or comp ).
The behavior of this function template is equivalent to:
| |
std::binary_search
<algorithm>
template <class ForwardIterator, class T> bool binary_search ( ForwardIterator first, ForwardIterator last, const T& value ); template <class ForwardIterator, class T, class Compare> bool binary_search ( ForwardIterator first, ForwardIterator last, const T& value, Compare comp );
Test if value exists in sorted array
Returns
true
if an element in the range
[first,last)
is equivalent to
value
, and
false
otherwise.
The comparison is performed using either operator< for the first version, or comp for the second: A value, a , is considered equivalent to another, b , when (!(a<b) && !(b<a)) or (!comp(a,b) && !comp(b,a))
For the function to yield the expected result, the elements in the range shall already be ordered according to the same criterion ( operator< or comp ).
The behavior of this function template is equivalent to:
| |
| |
Output:
looking for a 3... found! looking for a 6... not found. |