Skip to main content
Ctrl+K
CUDA Core Compute Libraries - Home CUDA Core Compute Libraries - Home

CUDA Core Compute Libraries

  • GitHub
CUDA Core Compute Libraries - Home CUDA Core Compute Libraries - Home

CUDA Core Compute Libraries

  • GitHub

Table of Contents

  • CUDA C++ Core Libraries
    • libcu++
      • Overview
      • Setup
        • Requirements
        • Getting libcu++
        • Building & Testing libcu++
      • Releases
        • Changelog
        • Versioning
      • Standard API
        • Algorithms Library
        • C Library
        • Concepts Library
        • Container Library
        • Execution Library
        • Numerics Library
        • Ranges Library
        • Synchronization Library
        • Time Library
        • Type Support Library
        • Utility Library
      • Extended API
        • Bit
        • Execution model
        • Exception Handling
        • Memory model
        • Thread Groups
        • Synchronization Primitives
        • Asynchronous Operations
        • Memory access properties
        • Functional
        • Fancy Iterators
        • Type traits
        • Vector Tuple Protocol
        • Numeric
        • Random
        • Memory
        • Memory Resources
        • Math
        • Mdspan
        • Tensor Memory Accelerator (TMA)
        • Warp
        • Utility
        • Work stealing
      • Runtime
        • CUDA Runtime interactions
        • Streams
        • Events
        • Algorithm
        • Devices
        • Hierarchy
        • Launch
        • Buffer
        • Memory Pools
        • Legacy resources
      • PTX API
        • Examples
        • PTX Instructions
        • PTX Pragmas
      • API reference
    • CUB
      • Overview
      • CUB Tests
      • CUB Benchmarks
      • CUB Tunings
      • CUB Developer Overview
        • Thread-level
        • Warp-level
        • Block-scope
        • Device-scope
        • NVTX
      • CUB Releases
        • CUB 2.1.0
      • API documentation
        • Thread-level Primitives
        • Warp-Wide “Collective” Primitives
        • Block-Wide “Collective” Primitives
        • Device-Wide Primitives
      • API reference
    • Thrust
      • Overview
      • Thrust Developer Overview
        • Developer CMake Options
        • Thrust systems
      • Releases
        • Changelog
        • Versioning
      • Release Process
      • API documentation
        • Algorithms
        • Containers
        • Function Objects
        • Iterators
        • Memory Management
        • Numerics
        • Parallel Execution Policies
        • Random Number Generators
        • System
        • Utility
      • API reference
    • CUDA Experimental
      • Overview
      • Containers library
        • cuda::experimental::uninitialized_buffer
      • Memory Resources
        • basic_any_resource
        • memory_pool_properties
        • device_memory_pool
        • pinned_memory_pool
        • managed_memory_pool
        • legacy_pinned_memory_resource
        • legacy_managed_memory_resource
        • shared_resource
      • Graphs library
        • cuda::experimental::graph
        • cuda::experimental::graph_builder
        • cuda::experimental::graph_builder_ref
        • cuda::experimental::graph_node_ref
        • cuda::experimental::stf::graphed_interface_of
        • cuda::experimental::stf::graphed_interface_of< mdspan< T, P… > >
        • cuda::experimental::stf::graphed_interface_of< scalar_view< T > >
        • cuda::experimental::stf::graphed_interface_of< void_interface >
      • CUDASTF
        • Implementation of the matrix class
        • Lower-level API
      • API reference
    • Tensor Memory Accelerator (TMA)
    • CCCL 2.x ‐ CCCL 3.0 migration guide
    • CCCL Development Guide
      • CCCL Internal Macros
      • CCCL Testing Utilities
      • Build and Bisect Utilities
      • Symbol Visibility
        • Host Stub Visibility Issue
        • Device Kernel Visibility Issue
        • Linking TUs compiled with different architectures
    • Contributing to the CUDA Core Compute Libraries
      • Code of Conduct
    • License
  • CCCL Python Libraries
    • Setup and Installation
    • cuda.compute: Parallel Computing Primitives
    • cuda.coop: Cooperative Algorithms
    • Resources
    • API Reference
      • cuda.compute API Reference
      • cuda.coop API Reference
  • CUDA C++ Core Libraries
  • Thrust
  • Thrust API documentation
  • Algorithms
  • Searching
  • Binary Search
  • Binary Search
  • thrust::binary_search

thrust::binary_search#

Overloads#

binary_search(exec, first, last, value)#

template<typename DerivedPolicy, typename ForwardIterator, typename LessThanComparable>
bool thrust::binary_search(
const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
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 algorithm’s execution is parallelized as determined by exec.

The following code snippet demonstrates how to use binary_search to search for values in a ordered range using the thrust::device execution policy for parallelization:

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

Added in version 2.2.0.

See also

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

See also

lower_bound

See also

upper_bound

See also

equal_range

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • value – The value to be searched.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • 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.

binary_search(first, last, value)#

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

Added in version 2.2.0.

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.

binary_search(exec, first, last, value, comp)#

template<typename DerivedPolicy, typename ForwardIterator, typename T, typename StrictWeakOrdering>
bool thrust::binary_search(
const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
ForwardIterator first,
ForwardIterator last,
const T &value,
StrictWeakOrdering comp
)#

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 comp(*i, value) and comp(value, *i) are both false.

The algorithm’s execution is parallelized as determined by exec.

The following code snippet demonstrates how to use binary_search to search for values in a ordered range using the thrust::device execution policy for parallelization:

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

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

using ::cuda::std::less;

thrust::binary_search(thrust::device, input.begin(), input.end(), 0, less<int>()); // returns true
thrust::binary_search(thrust::device, input.begin(), input.end(), 1, less<int>()); // returns false
thrust::binary_search(thrust::device, input.begin(), input.end(), 2, less<int>()); // returns true
thrust::binary_search(thrust::device, input.begin(), input.end(), 3, less<int>()); // returns false
thrust::binary_search(thrust::device, input.begin(), input.end(), 8, less<int>()); // returns true
thrust::binary_search(thrust::device, input.begin(), input.end(), 9, less<int>()); // returns false

Added in version 2.2.0.

See also

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

See also

lower_bound

See also

upper_bound

See also

equal_range

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the ordered sequence.

  • last – The end of the ordered sequence.

  • value – The value to be searched.

  • comp – The comparison operator.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator – is a model of Forward Iterator.

  • T – is comparable to ForwardIterator's value_type.

  • StrictWeakOrdering – is a model of Strict Weak Ordering.

Returns:

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

binary_search(first, last, value, comp)#

template<class ForwardIterator, class T, class StrictWeakOrdering>
bool thrust::binary_search(
ForwardIterator first,
ForwardIterator last,
const T &value,
StrictWeakOrdering comp
)#

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 comp(*i, value) and comp(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>
#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;

using ::cuda::std::less;

thrust::binary_search(input.begin(), input.end(), 0, less<int>()); // returns true
thrust::binary_search(input.begin(), input.end(), 1, less<int>()); // returns false
thrust::binary_search(input.begin(), input.end(), 2, less<int>()); // returns true
thrust::binary_search(input.begin(), input.end(), 3, less<int>()); // returns false
thrust::binary_search(input.begin(), input.end(), 8, less<int>()); // returns true
thrust::binary_search(input.begin(), input.end(), 9, less<int>()); // returns false

Added in version 2.2.0.

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.

  • comp – The comparison operator.

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

  • T – is comparable to ForwardIterator's value_type.

  • StrictWeakOrdering – is a model of Strict Weak Ordering.

Returns:

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

binary_search(exec, first, last, values_first, values_last, result)#

template<typename DerivedPolicy, typename ForwardIterator, typename InputIterator, typename OutputIterator>
OutputIterator thrust::binary_search(
const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
ForwardIterator first,
ForwardIterator last,
InputIterator values_first,
InputIterator values_last,
OutputIterator result
)#

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.

The algorithm’s execution is parallelized as determined by exec.

The following code snippet demonstrates how to use binary_search to search for multiple values in a ordered range using the thrust::device execution policy for parallelization:

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.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(thrust::device,
                      input.begin(), input.end(),
                      values.begin(), values.end(),
                      output.begin());

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

Added in version 2.2.0.

See also

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

See also

lower_bound

See also

upper_bound

See also

equal_range

Parameters:
  • exec – The execution policy to use for parallelization.

  • 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.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • 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.

Pre:

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

binary_search(first, last, values_first, values_last, result)#

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

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.

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>
...
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());

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

Added in version 2.2.0.

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.

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.

Pre:

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

binary_search(exec, first, last, values_first, values_last, result, comp)#

template<typename DerivedPolicy, typename ForwardIterator, typename InputIterator, typename OutputIterator, typename StrictWeakOrdering>
OutputIterator thrust::binary_search(
const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
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 algorithm’s execution is parallelized as determined by exec.

The following code snippet demonstrates how to use binary_search to search for multiple values in a ordered range using the thrust::device execution policy for parallelization:

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.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(thrust::device,
                      input.begin(), input.end(),
                      values.begin(), values.end(),
                      output.begin(),
                      ::cuda::std::less<T>());

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

Added in version 2.2.0.

See also

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

See also

lower_bound

See also

upper_bound

See also

equal_range

Parameters:
  • exec – The execution policy to use for parallelization.

  • 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:
  • DerivedPolicy – The name of the derived execution policy.

  • 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.

binary_search(first, last, values_first, values_last, result, comp)#

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]

Added in version 2.2.0.

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.

previous

thrust::upper_bound

next

thrust::equal_range

On this page
  • Overloads
    • binary_search(exec, first, last, value)
      • binary_search()
    • binary_search(first, last, value)
      • binary_search()
    • binary_search(exec, first, last, value, comp)
      • binary_search()
    • binary_search(first, last, value, comp)
      • binary_search()
    • binary_search(exec, first, last, values_first, values_last, result)
      • binary_search()
    • binary_search(first, last, values_first, values_last, result)
      • binary_search()
    • binary_search(exec, first, last, values_first, values_last, result, comp)
      • binary_search()
    • binary_search(first, last, values_first, values_last, result, comp)
      • binary_search()

Copyright © 2026, NVIDIA Corporation.

Created using Sphinx 9.1.0.