Template Function thrust::max_element(ForwardIterator, ForwardIterator, BinaryPredicate)¶
Function Documentation¶
-
template<typename
ForwardIterator
, typenameBinaryPredicate
>
ForwardIteratorthrust
::
max_element
(ForwardIterator first, ForwardIterator last, BinaryPredicate comp) max_element
finds the largest element in the range[first, last)
. It returns the first iteratori
in[first, last)
such that no other iterator in[first, last)
points to a value larger than*i
. The return value islast
if and only if[first, last)
is an empty range.The two versions of
max_element
differ in how they define whether one element is less than another. This version compares objects using a function objectcomp
. Specifically, this version ofmax_element
returns the first iteratori
in[first, last)
such that, for every iteratorj
in[first, last)
,comp(*i, *j)
isfalse
.The following code snippet demonstrates how to use
max_element
to find the largest element of a collection of key-value pairs.- Return
An iterator pointing to the largest element 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, andForwardIterator's
value_type
is convertible to bothcomp's
first_argument_type
andsecond_argument_type
.BinaryPredicate
: is a model of Binary Predicate.
#include <thrust/extrema.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 *largest = thrust::max_element(data, data + 4, compare_key_value()); // largest == data + 3 // *largest == {6,1}