thrust::min_element

Defined in thrust/extrema.h

template<typename DerivedPolicy, typename ForwardIterator, typename BinaryPredicate>
ForwardIterator thrust::min_element(const thrust::detail::execution_policy_base<DerivedPolicy> &exec, ForwardIterator first, ForwardIterator last, BinaryPredicate comp)

min_element finds the smallest element in the range [first, last). It returns the first iterator i in [first, last) such that no other iterator in [first, last) points to a value smaller than *i. The return value is last if and only if [first, last) is an empty range.

The two versions of min_element differ in how they define whether one element is less than another. This version compares objects using a function object comp. Specifically, this version of min_element returns the first iterator i in [first, last) such that, for every iterator j in [first, last), comp(*j, *i) is false.

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

The following code snippet demonstrates how to use min_element to find the smallest element of a collection of key-value pairs using the thrust::host execution policy for parallelization:

#include <thrust/extrema.h>
#include <thrust/execution_policy.h>
...

struct key_value
{
  int key;
  int value;
};

struct compare_key_value
{
  __host__ __device__
  bool operator()(key_value lhs, key_value rhs)
  {
    return lhs.key < rhs.key;
  }
};

...
key_value data[4] = { {4,5}, {0,7}, {2,3}, {6,1} };

key_value *smallest = thrust::min_element(thrust::host, data, data + 4, compare_key_value());

// smallest == data + 1
// *smallest == {0,7}

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

  • first – The beginning of the sequence.

  • last – The end of the sequence.

  • comp – A binary predicate used for comparison.

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

  • ForwardIterator – is a model of Forward Iterator, and ForwardIterator's value_type is convertible to both comp's first_argument_type and second_argument_type.

  • BinaryPredicate – is a model of Binary Predicate.

Returns

An iterator pointing to the smallest element of the range [first, last), if it is not an empty range; last, otherwise.