Template Function thrust::minmax_element(ForwardIterator, ForwardIterator, BinaryPredicate)

Function Documentation

template<typename ForwardIterator, typename BinaryPredicate>
thrust::pair<ForwardIterator, ForwardIterator> thrust::minmax_element(ForwardIterator first, ForwardIterator last, BinaryPredicate comp)

minmax_element finds the smallest and largest elements in the range [first, last). It returns a pair of iterators (imin, imax) where imin is the same iterator returned by min_element and imax is the same iterator returned by max_element. This function is potentially more efficient than separate calls to min_element and max_element.

The following code snippet demonstrates how to use

minmax_element to find the smallest and largest elements of a collection of key-value pairs.
Return

A pair of iterator pointing to the smallest and largest elements of the range [first, last), if it is not an empty range; last, otherwise.

Parameters
  • first: The beginning of the sequence.

  • last: The end of the sequence.

  • comp: A binary predicate used for comparison.

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

#include <thrust/extrema.h>
#include <thrust/pair.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} };

thrust::pair<key_value*,key_value*> extrema = thrust::minmax_element(data, data + 4, compare_key_value());

// extrema.first   == data + 1
// *extrema.first  == {0,7}
// extrema.second  == data + 3
// *extrema.second == {6,1}

See

min_element

See

max_element

See

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1840.pdf