Logo
  • Overview
  • Releases
  • Release Process
  • Developer Build
  • Thrust API documentation
    • Algorithms
      • Copying
      • Merging
      • Prefix sums
      • Reductions
      • Reordering
      • Searching
        • Binary Search
          • thrust::equal_range
          • thrust::upper_bound
          • thrust::equal_range
          • thrust::lower_bound
          • thrust::equal_range
          • thrust::lower_bound
          • thrust::upper_bound
          • thrust::equal_range
          • thrust::binary_search
          • thrust::upper_bound
          • thrust::binary_search
          • thrust::upper_bound
          • thrust::lower_bound
          • thrust::lower_bound
          • thrust::binary_search
          • thrust::binary_search
        • thrust::find
        • thrust::find_if_not
        • thrust::mismatch
        • thrust::mismatch
        • thrust::mismatch
        • thrust::mismatch
        • thrust::find_if_not
        • thrust::partition_point
        • thrust::find
        • thrust::find_if
        • thrust::partition_point
        • thrust::find_if
      • Set Operations
      • Sorting
      • Transformations
    • Containers
    • Function Objects
    • Iterators
    • Memory Management
    • Numerics
    • Parallel Execution Policies
    • Random Number Generators
    • System
    • Utility
  • Thrust: The C++ Parallel Algorithms Library API
thrust
  • »
  • Thrust API documentation »
  • Algorithms »
  • Searching »
  • Binary Search »
  • thrust::binary_search

thrust::binary_search

Defined in thrust/binary_search.h

template<class ForwardIterator, class LessThanComparable>
bool thrust::binary_search(ForwardIterator first, ForwardIterator last, const LessThanComparable &value)

binary_search is a version of binary search: it attempts to find the element value 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. Specifically, this version returns true if and only if there exists an iterator i in [first, last) such that *i < value and value < *i are both false.

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

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

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

thrust::binary_search(input.begin(), input.end(), 0); // returns true
thrust::binary_search(input.begin(), input.end(), 1); // returns false
thrust::binary_search(input.begin(), input.end(), 2); // returns true
thrust::binary_search(input.begin(), input.end(), 3); // returns false
thrust::binary_search(input.begin(), input.end(), 8); // returns true
thrust::binary_search(input.begin(), input.end(), 9); // returns 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.

  • value – The value to be searched.

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

  • LessThanComparable – is a model of LessThanComparable.

Returns

true if an equivalent element exists in [first, last), otherwise false.


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