thrust

◆ replace_copy_if() [1/4]

template<typename DerivedPolicy , typename InputIterator , typename OutputIterator , typename Predicate , typename T >
__host__ __device__ OutputIterator thrust::replace_copy_if ( const thrust::detail::execution_policy_base< DerivedPolicy > &  exec,
InputIterator  first,
InputIterator  last,
OutputIterator  result,
Predicate  pred,
const T &  new_value 
)

replace_copy_if copies elements from the range [first, last) to the range [result, result + (last-first)), except that any element for which pred is true is not copied; new_value is copied instead.

More precisely, for every integer n such that 0 <= n < last-first, replace_copy_if performs the assignment *(result+n) = new_value if pred(*(first+n)), and *(result+n) = *(first+n) otherwise.

The algorithm's execution is parallelized as determined by exec.

Parameters
execThe execution policy to use for parallelization.
firstThe beginning of the sequence to copy from.
lastThe end of the sequence to copy from.
resultThe beginning of the sequence to copy to.
predThe predicate to test on every value of the range [first,last).
new_valueThe replacement value to assign pred(*i) evaluates to true.
Returns
result + (last-first)
Template Parameters
DerivedPolicyThe name of the derived execution policy.
InputIteratoris a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument_type.
OutputIteratoris a model of Output Iterator.
Predicateis a model of Predicate.
Tis a model of Assignable, and T is convertible to OutputIterator's value_type.
Precondition
first may equal result, but the ranges [first, last) and [result, result + (last - first)) shall not overlap otherwise.
#include <thrust/replace.h>
struct is_less_than_zero
{
__host__ __device__
bool operator()(int x)
{
return x < 0;
}
};
...
thrust::device_vector<int> A(4);
A[0] = 1;
A[1] = -3;
A[2] = 2;
A[3] = -1;
is_less_than_zero pred;
thrust::replace_copy_if(thrust::device, A.begin(), A.end(), B.begin(), pred, 0);
// B contains [1, 0, 2, 0]
See also
http://www.sgi.com/tech/stl/replace_copy_if.html
replace
replace_if
replace_copy