Template Function thrust::count_if(InputIterator, InputIterator, Predicate)¶
Function Documentation¶
-
template<typename
InputIterator
, typenamePredicate
>
thrust::iterator_traits<InputIterator>::difference_typethrust
::
count_if
(InputIterator first, InputIterator last, Predicate pred) count_if
finds the number of elements in[first,last)
for which a predicate istrue
. More precisely,count_if
returns the number of iteratorsi
in[first, last)
such thatpred(*i) == true
.The following code snippet demonstrates how to use
count
to count the number of odd numbers in a range.#include <thrust/count.h> #include <thrust/device_vector.h> ... struct is_odd { __host__ __device__ bool operator()(int &x) { return x & 1; } }; ... // fill a device_vector with even & odd numbers thrust::device_vector<int> vec(5); vec[0] = 0; vec[1] = 1; vec[2] = 2; vec[3] = 3; vec[4] = 4; // count the odd elements in vec int result = thrust::count_if(vec.begin(), vec.end(), is_odd()); // result == 2
- Return
The number of elements where
pred
istrue
.- Parameters
first
: The beginning of the sequence.last
: The end of the sequence.pred
: The predicate.
- Template Parameters
InputIterator
: must be a model of Input Iterator andInputIterator's
value_type
must be convertible toPredicate's
argument_type
.Predicate
: must be a model of Predicate.