libstdc++
|
![]() |
These algorithms are variations of a classic binary search, and all assume that the sequence being searched is already sorted.
The number of comparisons will be logarithmic (and as few as possible). The number of steps through the sequence will be logarithmic for random-access iterators (e.g., pointers), and linear otherwise.
The LWG has passed Defect Report 270, which notes: The proposed resolution reinterprets binary search. Instead of thinking about searching for a value in a sorted range, we view that as an important special case of a more general algorithm: searching for the partition point in a partitioned range. We also add a guarantee that the old wording did not: we ensure that the upper bound is no earlier than the lower bound, that the pair returned by equal_range is a valid range, and that the first part of that pair is the lower bound.
The actual effect of the first sentence is that a comparison functor passed by the user doesn't necessarily need to induce a strict weak ordering relation. Rather, it partitions the range.
bool std::binary_search | ( | _ForwardIterator | __first, |
_ForwardIterator | __last, | ||
const _Tp & | __val | ||
) |
Determines whether an element exists in a range.
first | An iterator. |
last | Another iterator. |
val | The search term. |
Note that this does not actually return an iterator to val. For that, use std::find or a container's specialized find member functions.
Definition at line 2665 of file stl_algo.h.
References std::lower_bound().
bool std::binary_search | ( | _ForwardIterator | __first, |
_ForwardIterator | __last, | ||
const _Tp & | __val, | ||
_Compare | __comp | ||
) |
Determines whether an element exists in a range.
first | An iterator. |
last | Another iterator. |
val | The search term. |
comp | A functor to use for comparisons. |
Note that this does not actually return an iterator to val. For that, use std::find or a container's specialized find member functions.
The comparison function should have the same effects on ordering as the function used for the initial sort.
Definition at line 2698 of file stl_algo.h.
References std::lower_bound().
pair<_ForwardIterator, _ForwardIterator> std::equal_range | ( | _ForwardIterator | __first, |
_ForwardIterator | __last, | ||
const _Tp & | __val | ||
) |
Finds the largest subrange in which val could be inserted at any place in it without changing the ordering.
first | An iterator. |
last | Another iterator. |
val | The search term. |
This is equivalent to
std::make_pair(lower_bound(first, last, val), upper_bound(first, last, val))
but does not actually call those functions.
Definition at line 2543 of file stl_algo.h.
References std::distance(), std::advance(), std::lower_bound(), and std::upper_bound().
pair<_ForwardIterator, _ForwardIterator> std::equal_range | ( | _ForwardIterator | __first, |
_ForwardIterator | __last, | ||
const _Tp & | __val, | ||
_Compare | __comp | ||
) |
Finds the largest subrange in which val could be inserted at any place in it without changing the ordering.
first | An iterator. |
last | Another iterator. |
val | The search term. |
comp | A functor to use for comparisons. |
This is equivalent to
std::make_pair(lower_bound(first, last, val, comp), upper_bound(first, last, val, comp))
but does not actually call those functions.
Definition at line 2605 of file stl_algo.h.
References std::distance(), std::advance(), std::lower_bound(), and std::upper_bound().
_ForwardIterator std::lower_bound | ( | _ForwardIterator | __first, |
_ForwardIterator | __last, | ||
const _Tp & | __val | ||
) |
Finds the first position in which val could be inserted without changing the ordering.
first | An iterator. |
last | Another iterator. |
val | The search term. |
Definition at line 936 of file stl_algobase.h.
References std::distance(), and std::advance().
Referenced by std::equal_range(), std::binary_search(), std::__merge_adaptive(), and std::__merge_without_buffer().
_ForwardIterator std::lower_bound | ( | _ForwardIterator | __first, |
_ForwardIterator | __last, | ||
const _Tp & | __val, | ||
_Compare | __comp | ||
) |
Finds the first position in which val could be inserted without changing the ordering.
first | An iterator. |
last | Another iterator. |
val | The search term. |
comp | A functor to use for comparisons. |
The comparison function should have the same effects on ordering as the function used for the initial sort.
Definition at line 2394 of file stl_algo.h.
References std::distance(), and std::advance().
_ForwardIterator std::upper_bound | ( | _ForwardIterator | __first, |
_ForwardIterator | __last, | ||
const _Tp & | __val | ||
) |
Finds the last position in which val could be inserted without changing the ordering.
first | An iterator. |
last | Another iterator. |
val | The search term. |
Definition at line 2441 of file stl_algo.h.
References std::distance(), and std::advance().
_ForwardIterator std::upper_bound | ( | _ForwardIterator | __first, |
_ForwardIterator | __last, | ||
const _Tp & | __val, | ||
_Compare | __comp | ||
) |
Finds the last position in which val could be inserted without changing the ordering.
first | An iterator. |
last | Another iterator. |
val | The search term. |
comp | A functor to use for comparisons. |
The comparison function should have the same effects on ordering as the function used for the initial sort.
Definition at line 2490 of file stl_algo.h.
References std::distance(), and std::advance().
Referenced by std::equal_range(), std::__merge_adaptive(), and std::__merge_without_buffer().