Binary search algorithms


Functions

template<typename _ForwardIter, typename _Tp> _ForwardIter std::lower_bound (_ForwardIter __first, _ForwardIter __last, const _Tp &__val)
 Finds the first position in which val could be inserted without changing the ordering.

template<typename _ForwardIter, typename _Tp, typename _Compare> _ForwardIter std::lower_bound (_ForwardIter __first, _ForwardIter __last, const _Tp &__val, _Compare __comp)
 Finds the first position in which val could be inserted without changing the ordering.

template<typename _ForwardIter, typename _Tp> _ForwardIter std::upper_bound (_ForwardIter __first, _ForwardIter __last, const _Tp &__val)
 Finds the last position in which val could be inserted without changing the ordering.

template<typename _ForwardIter, typename _Tp, typename _Compare> _ForwardIter std::upper_bound (_ForwardIter __first, _ForwardIter __last, const _Tp &__val, _Compare __comp)
 Finds the last position in which val could be inserted without changing the ordering.

template<typename _ForwardIter, typename _Tp> pair< _ForwardIter, _ForwardIter > std::equal_range (_ForwardIter __first, _ForwardIter __last, const _Tp &__val)
 Finds the largest subrange in which val could be inserted at any place in it without changing the ordering.

template<typename _ForwardIter, typename _Tp, typename _Compare> pair< _ForwardIter, _ForwardIter > std::equal_range (_ForwardIter __first, _ForwardIter __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.

template<typename _ForwardIter, typename _Tp> bool std::binary_search (_ForwardIter __first, _ForwardIter __last, const _Tp &__val)
 Determines whether an element exists in a range.

template<typename _ForwardIter, typename _Tp, typename _Compare> bool std::binary_search (_ForwardIter __first, _ForwardIter __last, const _Tp &__val, _Compare __comp)
 Determines whether an element exists in a range.


Detailed Description

These algorithms are variations of a classic binary search. They 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.


Function Documentation

template<typename _ForwardIter, typename _Tp, typename _Compare>
bool binary_search _ForwardIter  __first,
_ForwardIter  __last,
const _Tp &  __val,
_Compare  __comp
 

Determines whether an element exists in a range.

Parameters:
first An iterator.
last Another iterator.
val The search term.
comp A functor to use for comparisons.
Returns:
True if val (or its equivelent) is in [first,last ].
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 3086 of file stl_algo.h.

References std::lower_bound().

template<typename _ForwardIter, typename _Tp>
bool binary_search _ForwardIter  __first,
_ForwardIter  __last,
const _Tp &  __val
 

Determines whether an element exists in a range.

Parameters:
first An iterator.
last Another iterator.
val The search term.
Returns:
True if val (or its equivelent) is in [first,last ].
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 3055 of file stl_algo.h.

References std::lower_bound().

template<typename _ForwardIter, typename _Tp, typename _Compare>
pair<_ForwardIter, _ForwardIter> equal_range _ForwardIter  __first,
_ForwardIter  __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.

Parameters:
first An iterator.
last Another iterator.
val The search term.
comp A functor to use for comparisons.
Returns:
An pair of iterators defining the subrange.
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 3006 of file stl_algo.h.

References std::advance(), std::distance(), std::lower_bound(), and std::upper_bound().

template<typename _ForwardIter, typename _Tp>
pair<_ForwardIter, _ForwardIter> equal_range _ForwardIter  __first,
_ForwardIter  __last,
const _Tp &  __val
 

Finds the largest subrange in which val could be inserted at any place in it without changing the ordering.

Parameters:
first An iterator.
last Another iterator.
val The search term.
Returns:
An pair of iterators defining the subrange.
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 2951 of file stl_algo.h.

References std::advance(), std::distance(), std::lower_bound(), and std::upper_bound().

template<typename _ForwardIter, typename _Tp, typename _Compare>
_ForwardIter lower_bound _ForwardIter  __first,
_ForwardIter  __last,
const _Tp &  __val,
_Compare  __comp
 

Finds the first position in which val could be inserted without changing the ordering.

Parameters:
first An iterator.
last Another iterator.
val The search term.
comp A functor to use for comparisons.
Returns:
An iterator pointing to the first element "not less than" val.
The comparison function should have the same effects on ordering as the function used for the initial sort.

Definition at line 2819 of file stl_algo.h.

References std::advance(), and std::distance().

template<typename _ForwardIter, typename _Tp>
_ForwardIter lower_bound _ForwardIter  __first,
_ForwardIter  __last,
const _Tp &  __val
 

Finds the first position in which val could be inserted without changing the ordering.

Parameters:
first An iterator.
last Another iterator.
val The search term.
Returns:
An iterator pointing to the first element "not less than" val.

Definition at line 2771 of file stl_algo.h.

References std::advance(), and std::distance().

Referenced by std::binary_search(), std::equal_range(), and std::map< _Key, _Tp, _Compare, _Alloc >::operator[]().

template<typename _ForwardIter, typename _Tp, typename _Compare>
_ForwardIter upper_bound _ForwardIter  __first,
_ForwardIter  __last,
const _Tp &  __val,
_Compare  __comp
 

Finds the last position in which val could be inserted without changing the ordering.

Parameters:
first An iterator.
last Another iterator.
val The search term.
comp A functor to use for comparisons.
Returns:
An iterator pointing to the first element greater than val.
The comparison function should have the same effects on ordering as the function used for the initial sort.

Definition at line 2904 of file stl_algo.h.

References std::advance(), and std::distance().

template<typename _ForwardIter, typename _Tp>
_ForwardIter upper_bound _ForwardIter  __first,
_ForwardIter  __last,
const _Tp &  __val
 

Finds the last position in which val could be inserted without changing the ordering.

Parameters:
first An iterator.
last Another iterator.
val The search term.
Returns:
An iterator pointing to the first element greater than val.

Definition at line 2859 of file stl_algo.h.

References std::advance(), and std::distance().

Referenced by std::equal_range().


Generated on Wed Sep 29 13:54:56 2004 for libstdc++-v3 Source by doxygen 1.3.7