thrust::count_if
Defined in thrust/count.h
- 
template<typename DerivedPolicy, typename InputIterator, typename Predicate>
 thrust::detail::it_difference_t<InputIterator> thrust::count_if(const thrust::detail::execution_policy_base<DerivedPolicy> &exec, InputIterator first, InputIterator last, Predicate pred)
- count_iffinds the number of elements in- [first,last)for which a predicate is- true. More precisely,- count_ifreturns the number of iterators- iin- [first, last)such that- pred(*i) == true.- The algorithm’s execution is parallelized as determined by - exec.- The following code snippet demonstrates how to use - countto count the number of odd numbers in a range using the- thrust::deviceexecution policy:- #include <thrust/count.h> #include <thrust/device_vector.h> #include <thrust/execution_policy.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(thrust::device, vec.begin(), vec.end(), is_odd()); // result == 2 - Parameters
- exec – The execution policy to use for parallelization. 
- first – The beginning of the sequence. 
- last – The end of the sequence. 
- pred – The predicate. 
 
- Template Parameters
- DerivedPolicy – The name of the derived execution policy. 
- InputIterator – must be a model of Input Iterator and - InputIterator's- value_typemust be convertible to- Predicate'sargument type.
- Predicate – must be a model of Predicate. 
 
- Returns
- The number of elements where - predis- true.