thrust::copy_if
Defined in thrust/copy.h
-
template<typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename Predicate>
OutputIterator thrust::copy_if(const thrust::detail::execution_policy_base<DerivedPolicy> &exec, InputIterator first, InputIterator last, OutputIterator result, Predicate pred) This version of
copy_if
copies elements from the range[first,last)
to a range beginning atresult
, except that any element which causespred
to befalse
is not copied.copy_if
is stable, meaning that the relative order of elements that are copied is unchanged.More precisely, for every integer
n
such that0 <= n < last-first
,copy_if
performs the assignment*result = *(first+n)
andresult
is advanced one position ifpred(*(first+n))
. Otherwise, no assignment occurs andresult
is not advanced.The algorithm’s execution is parallelized as determined by
system
.The following code snippet demonstrates how to use
copy_if
to perform stream compaction to copy even numbers to an output range using thethrust::host
parallelization policy:#include <thrust/copy.h> #include <thrust/execution_policy.h> ... struct is_even { __host__ __device__ bool operator()(const int x) { return (x % 2) == 0; } }; ... const int N = 6; int V[N] = {-2, 0, -1, 0, 1, 2}; int result[4]; thrust::copy_if(thrust::host, V, V + N, result, is_even()); // V remains {-2, 0, -1, 0, 1, 2} // result is now {-2, 0, 0, 2}
See also
remove_copy_if
- Parameters
exec – The execution policy to use for parallelization.
first – The beginning of the sequence from which to copy.
last – The end of the sequence from which to copy.
result – The beginning of the sequence into which to copy.
pred – The predicate to test on every value of the range
[first, last)
.
- Template Parameters
DerivedPolicy – The name of the derived execution policy.
InputIterator – is a model of Input Iterator, and
InputIterator's
value_type
is convertible toPredicate's
argument_type
.OutputIterator – is a model of Output Iterator.
Predicate – is a model of Predicate.
- Returns
result + n
, wheren
is equal to the number of timespred
evaluated totrue
in the range[first, last)
.- Pre
The ranges
[first, last)
and[result, result + (last - first))
shall not overlap.