std::ranges::binary_search
| Defined in header <algorithm>
|
||
| Call signature |
||
template< std::forward_iterator I, std::sentinel_for<I> S,
class T, class Proj = std::identity,
std::indirect_strict_weak_order
<const T*, std::projected<I, Proj>> Comp = ranges::less >
constexpr bool binary_search( I first, S last, const T& value,
Comp comp = {}, Proj proj = {} );
|
(1) | (since C++20) (until C++26) |
template< std::forward_iterator I, std::sentinel_for<I> S,
class Proj = std::identity,
class T = std::projected_value_t<I, Proj>,
std::indirect_strict_weak_order
<const T*, std::projected<I, Proj>> Comp = ranges::less >
constexpr bool binary_search( I first, S last, const T& value,
Comp comp = {}, Proj proj = {} );
|
(since C++26) | |
template< ranges::forward_range R,
class T, class Proj = std::identity,
std::indirect_strict_weak_order
<const T*, std::projected<ranges::iterator_t<R>,
Proj>> Comp = ranges::less >
constexpr bool binary_search( R&& r, const T& value,
Comp comp = {}, Proj proj = {} );
|
(2) | (since C++20) (until C++26) |
template< ranges::forward_range R,
class Proj = std::identity,
class T = std::projected_value_t<ranges::iterator_t<R>, Proj>,
std::indirect_strict_weak_order
<const T*, std::projected<ranges::iterator_t<R>,
Proj>> Comp = ranges::less >
constexpr bool binary_search( R&& r, const T& value,
Comp comp = {}, Proj proj = {} );
|
(since C++26) | |
Checks if an element equivalent to value exists in the partitioned source range [first, last) or r. An element is considered equivalent to value if its projected value neither orders before nor orders after value with the comparator comp.
If the elements e of the source range are not partitioned with respect the following expressions at the same time, the behavior is undefined:
bool(std::invoke(comp, std::invoke(proj, e), value))!bool(std::invoke(comp, value, std::invoke(proj, e)))
The function-like entities described on this page are algorithm function objects (informally known as niebloids), that is:
- Explicit template argument lists cannot be specified when calling any of them.
- None of them are visible to argument-dependent lookup.
- When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.
Parameters
| first, last | - | the iterator-sentinel pair defining the source range |
| r | - | the source range |
| value | - | the value to be compared with the (projected) elements |
| comp | - | the comparator to be applied to the (projected) elements |
| proj | - | the projection to be applied to the elements |
Return value
true if an element equivalent to value exists, false otherwise.
Complexity
Given N as ranges::distance(first, last) or ranges::distance(r):
comp and proj.Notes
ranges::binary_search doesn't return an iterator to the found element when an element whose projection equals value is found. To obtain an iterator to that element (if exists), ranges::lower_bound should be used instead.
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_algorithm_default_value_type |
202403 |
(C++26) | List-initialization for algorithms (1,2) |
Possible implementation
struct binary_search_fn
{
template<std::forward_iterator I, std::sentinel_for<I> S,
class Proj = std::identity, class T = std::projected_value_t<I, Proj>,
std::indirect_strict_weak_order
<const T*, std::projected<I, Proj>> Comp = ranges::less>
constexpr bool operator()(I first, S last, const T& value,
Comp comp = {}, Proj proj = {}) const
{
auto x = ranges::lower_bound(first, last, value, comp, proj);
return (!(x == last) && !(std::invoke(comp, value, std::invoke(proj, *x))));
}
template<ranges::forward_range R, class Proj = std::identity,
class T = std::projected_value_t<ranges::iterator_t<R>, Proj>,
std::indirect_strict_weak_order
<const T*, std::projected<ranges::iterator_t<R>,
Proj>> Comp = ranges::less>
constexpr bool operator()(R&& r, const T& value,
Comp comp = {}, Proj proj = {}) const
{
return (*this)(ranges::begin(r),
ranges::next(ranges::begin(r), ranges::end(r)),
value, std::move(comp), std::move(proj));
}
};
inline constexpr binary_search_fn binary_search;
|
Example
#include <algorithm>
#include <cassert>
#include <complex>
#include <iostream>
#include <ranges>
#include <vector>
int main()
{
constexpr static auto haystack = {1, 3, 4, 5, 9};
static_assert(std::ranges::is_sorted(haystack));
for (const int needle : std::views::iota(1)
| std::views::take(3))
{
std::cout << "Searching for " << needle << ": ";
std::ranges::binary_search(haystack, needle)
? std::cout << "found " << needle << '\n'
: std::cout << "no dice!\n";
}
using CD = std::complex<double>;
std::vector<CD> nums{{1, 1}, {2, 3}, {4, 2}, {4, 3}};
auto cmpz = [](CD x, CD y){ return abs(x) < abs(y); };
#ifdef __cpp_lib_algorithm_default_value_type
assert(std::ranges::binary_search(nums, {4, 2}, cmpz));
#else
assert(std::ranges::binary_search(nums, CD{4, 2}, cmpz));
#endif
}
Output:
Searching for 1: found 1
Searching for 2: no dice!
Searching for 3: found 3
See also
| determines if an element exists in a range using binary search (function template) | |
(C++20) |
finds the range of elements matching the given value using binary search (algorithm function object) |
(C++20) |
finds the first element not less than the given value using binary search (algorithm function object) |
(C++20) |
finds the first element greater than the given value using binary search (algorithm function object) |
(C++23)(C++23) |
checks if the range contains the given element or subrange (algorithm function object) |