Logo
  • Overview
  • Releases
  • Release Process
  • Developer Build
  • Thrust API documentation
  • Thrust: The C++ Parallel Algorithms Library API
    • Directory hierarchy
    • Namespace hierarchy
    • Defines
    • Directories
    • Namespaces
      • thrust
        • Classes
        • Functions
          • thrust::abs
          • thrust::acos
          • thrust::acosh
          • thrust::adjacent_difference
          • thrust::adjacent_difference
          • thrust::adjacent_difference
          • thrust::adjacent_difference
          • thrust::advance
          • thrust::all_of
          • thrust::all_of
          • thrust::allocate_unique
          • thrust::allocate_unique_n
          • thrust::any_of
          • thrust::any_of
          • thrust::arg
          • thrust::asin
          • thrust::asinh
          • thrust::atan
          • thrust::atanh
          • thrust::binary_search
          • thrust::binary_search
          • thrust::binary_search
          • thrust::binary_search
          • thrust::binary_search
          • thrust::binary_search
          • thrust::binary_search
          • thrust::binary_search
          • thrust::conj
          • thrust::constant_iterator
          • thrust::copy
          • thrust::copy
          • thrust::copy_if
          • thrust::copy_if
          • thrust::copy_if
          • thrust::copy_if
          • thrust::copy_n
          • thrust::copy_n
          • thrust::cos
          • thrust::cosh
          • thrust::count
          • thrust::count
          • thrust::count_if
          • thrust::count_if
          • thrust::device_delete
          • thrust::device_free
          • thrust::device_make_unique
          • thrust::device_malloc
          • thrust::device_new
          • thrust::device_new
          • thrust::device_new
          • thrust::device_pointer_cast
          • thrust::device_pointer_cast
          • thrust::distance
          • thrust::equal
          • thrust::equal
          • thrust::equal
          • thrust::equal
          • thrust::equal_range
          • thrust::equal_range
          • thrust::equal_range
          • thrust::equal_range
          • thrust::exclusive_scan
          • thrust::exclusive_scan
          • thrust::exclusive_scan
          • thrust::exclusive_scan
          • thrust::exclusive_scan
          • thrust::exclusive_scan
          • thrust::exclusive_scan_by_key
          • thrust::exclusive_scan_by_key
          • thrust::exclusive_scan_by_key
          • thrust::exclusive_scan_by_key
          • thrust::exclusive_scan_by_key
          • thrust::exclusive_scan_by_key
          • thrust::exclusive_scan_by_key
          • thrust::exclusive_scan_by_key
          • thrust::exp
          • thrust::fill
          • thrust::fill
          • thrust::fill_n
          • thrust::fill_n
          • thrust::find
          • thrust::find
          • thrust::find_if
          • thrust::find_if
          • thrust::find_if_not
          • thrust::find_if_not
          • thrust::for_each
          • thrust::for_each
          • thrust::for_each_n
          • thrust::for_each_n
          • thrust::free
          • thrust::gather
          • thrust::gather
          • thrust::gather_if
          • thrust::gather_if
          • thrust::gather_if
          • thrust::gather_if
          • thrust::generate
          • thrust::generate
          • thrust::generate_n
          • thrust::generate_n
          • thrust::get_per_device_resource
          • thrust::get_temporary_buffer
          • thrust::inclusive_scan
          • thrust::inclusive_scan
          • thrust::inclusive_scan
          • thrust::inclusive_scan
          • thrust::inclusive_scan
          • thrust::inclusive_scan
          • thrust::inclusive_scan_by_key
          • thrust::inclusive_scan_by_key
          • thrust::inclusive_scan_by_key
          • thrust::inclusive_scan_by_key
          • thrust::inclusive_scan_by_key
          • thrust::inclusive_scan_by_key
          • thrust::inner_product
          • thrust::inner_product
          • thrust::inner_product
          • thrust::inner_product
          • thrust::is_partitioned
          • thrust::is_partitioned
          • thrust::is_sorted
          • thrust::is_sorted
          • thrust::is_sorted
          • thrust::is_sorted
          • thrust::is_sorted_until
          • thrust::is_sorted_until
          • thrust::is_sorted_until
          • thrust::is_sorted_until
          • thrust::log
          • thrust::log10
          • thrust::lower_bound
          • thrust::lower_bound
          • thrust::lower_bound
          • thrust::lower_bound
          • thrust::lower_bound
          • thrust::lower_bound
          • thrust::lower_bound
          • thrust::lower_bound
          • thrust::make_constant_iterator
          • thrust::make_constant_iterator
          • thrust::make_counting_iterator
          • thrust::make_discard_iterator
          • thrust::make_permutation_iterator
          • thrust::make_reverse_iterator
          • thrust::make_shuffle_iterator
          • thrust::make_tabulate_output_iterator
          • thrust::make_tagged_deleter
          • thrust::make_transform_input_output_iterator
          • thrust::make_transform_iterator
          • thrust::make_transform_output_iterator
          • thrust::make_zip_function
          • thrust::make_zip_iterator
          • thrust::make_zip_iterator
          • thrust::malloc
          • thrust::max_element
          • thrust::max_element
          • thrust::max_element
          • thrust::max_element
          • thrust::merge
          • thrust::merge
          • thrust::merge
          • thrust::merge
          • thrust::merge_by_key
          • thrust::merge_by_key
          • thrust::merge_by_key
          • thrust::merge_by_key
          • thrust::min_element
          • thrust::min_element
          • thrust::min_element
          • thrust::min_element
          • thrust::minmax_element
          • thrust::minmax_element
          • thrust::minmax_element
          • thrust::minmax_element
          • thrust::mismatch
          • thrust::mismatch
          • thrust::mismatch
          • thrust::mismatch
          • thrust::next
          • thrust::none_of
          • thrust::none_of
          • thrust::norm
          • thrust::offset_iterator
          • thrust::operator!=
          • thrust::operator!=
          • thrust::operator!=
          • thrust::operator!=
          • thrust::operator!=
          • thrust::operator*
          • thrust::operator*
          • thrust::operator*
          • thrust::operator+
          • thrust::operator+
          • thrust::operator+
          • thrust::operator+
          • thrust::operator-
          • thrust::operator-
          • thrust::operator-
          • thrust::operator-
          • thrust::operator/
          • thrust::operator/
          • thrust::operator/
          • thrust::operator<<
          • thrust::operator<<
          • thrust::operator<<
          • thrust::operator==
          • thrust::operator==
          • thrust::operator==
          • thrust::operator==
          • thrust::operator==
          • thrust::operator>>
          • thrust::partition
          • thrust::partition
          • thrust::partition
          • thrust::partition
          • thrust::partition_copy
          • thrust::partition_copy
          • thrust::partition_copy
          • thrust::partition_copy
          • thrust::partition_point
          • thrust::partition_point
          • thrust::polar
          • thrust::pow
          • thrust::pow
          • thrust::pow
          • thrust::prev
          • thrust::proj
          • thrust::raw_pointer_cast
          • thrust::raw_reference_cast
          • thrust::raw_reference_cast
          • thrust::reduce
          • thrust::reduce
          • thrust::reduce
          • thrust::reduce
          • thrust::reduce
          • thrust::reduce
          • thrust::reduce_by_key
          • thrust::reduce_by_key
          • thrust::reduce_by_key
          • thrust::reduce_by_key
          • thrust::reduce_by_key
          • thrust::reduce_by_key
          • thrust::reduce_into
          • thrust::reduce_into
          • thrust::reduce_into
          • thrust::reduce_into
          • thrust::reduce_into
          • thrust::reduce_into
          • thrust::remove
          • thrust::remove
          • thrust::remove_copy
          • thrust::remove_copy
          • thrust::remove_copy_if
          • thrust::remove_copy_if
          • thrust::remove_copy_if
          • thrust::remove_copy_if
          • thrust::remove_if
          • thrust::remove_if
          • thrust::remove_if
          • thrust::remove_if
          • thrust::replace
          • thrust::replace
          • thrust::replace_copy
          • thrust::replace_copy
          • thrust::replace_copy_if
          • thrust::replace_copy_if
          • thrust::replace_copy_if
          • thrust::replace_copy_if
          • thrust::replace_if
          • thrust::replace_if
          • thrust::replace_if
          • thrust::replace_if
          • thrust::return_temporary_buffer
          • thrust::reverse
          • thrust::reverse
          • thrust::reverse_copy
          • thrust::reverse_copy
          • thrust::scatter
          • thrust::scatter
          • thrust::scatter_if
          • thrust::scatter_if
          • thrust::scatter_if
          • thrust::scatter_if
          • thrust::sequence
          • thrust::sequence
          • thrust::sequence
          • thrust::sequence
          • thrust::sequence
          • thrust::sequence
          • thrust::set_difference
          • thrust::set_difference
          • thrust::set_difference
          • thrust::set_difference
          • thrust::set_difference_by_key
          • thrust::set_difference_by_key
          • thrust::set_difference_by_key
          • thrust::set_difference_by_key
          • thrust::set_intersection
          • thrust::set_intersection
          • thrust::set_intersection
          • thrust::set_intersection
          • thrust::set_intersection_by_key
          • thrust::set_intersection_by_key
          • thrust::set_intersection_by_key
          • thrust::set_intersection_by_key
          • thrust::set_symmetric_difference
          • thrust::set_symmetric_difference
          • thrust::set_symmetric_difference
          • thrust::set_symmetric_difference
          • thrust::set_symmetric_difference_by_key
          • thrust::set_symmetric_difference_by_key
          • thrust::set_symmetric_difference_by_key
          • thrust::set_symmetric_difference_by_key
          • thrust::set_union
          • thrust::set_union
          • thrust::set_union
          • thrust::set_union
          • thrust::set_union_by_key
          • thrust::set_union_by_key
          • thrust::set_union_by_key
          • thrust::set_union_by_key
          • thrust::shuffle
          • thrust::shuffle
          • thrust::shuffle_copy
          • thrust::shuffle_copy
          • thrust::sin
          • thrust::sinh
          • thrust::sort
          • thrust::sort
          • thrust::sort
          • thrust::sort
          • thrust::sort_by_key
          • thrust::sort_by_key
          • thrust::sort_by_key
          • thrust::sort_by_key
          • thrust::sqrt
          • thrust::stable_partition
          • thrust::stable_partition
          • thrust::stable_partition
          • thrust::stable_partition
          • thrust::stable_partition_copy
          • thrust::stable_partition_copy
          • thrust::stable_partition_copy
          • thrust::stable_partition_copy
          • thrust::stable_sort
          • thrust::stable_sort
          • thrust::stable_sort
          • thrust::stable_sort
          • thrust::stable_sort_by_key
          • thrust::stable_sort_by_key
          • thrust::stable_sort_by_key
          • thrust::stable_sort_by_key
          • thrust::swap_ranges
          • thrust::swap_ranges
          • thrust::tabulate
          • thrust::tabulate
          • thrust::tan
          • thrust::tanh
          • thrust::transform
          • thrust::transform
          • thrust::transform
          • thrust::transform
          • thrust::transform_exclusive_scan
          • thrust::transform_exclusive_scan
          • thrust::transform_if
          • thrust::transform_if
          • thrust::transform_if
          • thrust::transform_if
          • thrust::transform_if
          • thrust::transform_if
          • thrust::transform_inclusive_scan
          • thrust::transform_inclusive_scan
          • thrust::transform_inclusive_scan
          • thrust::transform_inclusive_scan
          • thrust::transform_reduce
          • thrust::transform_reduce
          • thrust::try_unwrap_contiguous_iterator
          • thrust::uninitialized_allocate_unique
          • thrust::uninitialized_allocate_unique_n
          • thrust::uninitialized_copy
          • thrust::uninitialized_copy
          • thrust::uninitialized_copy_n
          • thrust::uninitialized_copy_n
          • thrust::uninitialized_fill
          • thrust::uninitialized_fill
          • thrust::uninitialized_fill_n
          • thrust::uninitialized_fill_n
          • thrust::unique
          • thrust::unique
          • thrust::unique
          • thrust::unique
          • thrust::unique_by_key
          • thrust::unique_by_key
          • thrust::unique_by_key
          • thrust::unique_by_key
          • thrust::unique_by_key_copy
          • thrust::unique_by_key_copy
          • thrust::unique_by_key_copy
          • thrust::unique_by_key_copy
          • thrust::unique_copy
          • thrust::unique_copy
          • thrust::unique_copy
          • thrust::unique_copy
          • thrust::unique_count
          • thrust::unique_count
          • thrust::unique_count
          • thrust::unique_count
          • thrust::unwrap_contiguous_iterator
          • thrust::upper_bound
          • thrust::upper_bound
          • thrust::upper_bound
          • thrust::upper_bound
          • thrust::upper_bound
          • thrust::upper_bound
          • thrust::upper_bound
          • thrust::upper_bound
          • thrust::zip_iterator
        • Namespaces
        • Structs
        • Typedefs
        • Variables
thrust
  • »
  • Thrust: The C++ Parallel Algorithms Library API »
  • thrust »
  • thrust::binary_search

thrust::binary_search

Defined in thrust/binary_search.h

template<class ForwardIterator, class InputIterator, class OutputIterator, class StrictWeakOrdering>
OutputIterator thrust::binary_search(ForwardIterator first, ForwardIterator last, InputIterator values_first, InputIterator values_last, OutputIterator result, StrictWeakOrdering comp)

binary_search is a vectorized version of binary search: for each iterator v in [values_first, values_last) it attempts to find the value *v in an ordered range [first, last). It returns true if an element that is equivalent to value is present in [first, last) and false if no such element exists. This version of binary_search uses function object comp for comparison.

The following code snippet demonstrates how to use binary_search to search for multiple values in a ordered range.

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/functional.h>
...
thrust::device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

thrust::device_vector<int> values(6);
values[0] = 0;
values[1] = 1;
values[2] = 2;
values[3] = 3;
values[4] = 8;
values[5] = 9;

thrust::device_vector<bool> output(6);

thrust::binary_search(input.begin(), input.end(),
                      values.begin(), values.end(),
                      output.begin(),
                      ::cuda::std::less<T>());

// output is now [true, false, true, false, true, false]

See also

https://en.cppreference.com/w/cpp/algorithm/binary_search

See also

lower_bound

See also

upper_bound

See also

equal_range

Parameters
  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • values_first – The beginning of the search values sequence.

  • values_last – The end of the search values sequence.

  • result – The beginning of the output sequence.

  • comp – The comparison operator.

Template Parameters
  • ForwardIterator – is a model of Forward Iterator.

  • InputIterator – is a model of Input Iterator. and InputIterator's value_type is LessThanComparable.

  • OutputIterator – is a model of Output Iterator. and bool is convertible to OutputIterator's value_type.

  • StrictWeakOrdering – is a model of Strict Weak Ordering.

Pre

The ranges [first,last) and [result, result + (last - first)) shall not overlap.


© Copyright 2025, NVIDIA. Last updated on May 13, 2025.