◆ replace_copy_if() [4/4]

template<typename InputIterator1 , typename InputIterator2 , typename OutputIterator , typename Predicate , typename T >
OutputIterator thrust::replace_copy_if ( InputIterator1  first,
InputIterator1  last,
InputIterator2  stencil,
OutputIterator  result,
Predicate  pred,
const T &  new_value 

This version of replace_copy_if copies elements from the range [first, last) to the range [result, result + (last-first)), except that any element whose corresponding stencil element causes pred to be 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(*(stencil+n)), and *(result+n) = *(first+n) otherwise.

firstThe beginning of the sequence to copy from.
lastThe end of the sequence to copy from.
stencilThe beginning of the stencil sequence.
resultThe beginning of the sequence to copy to.
predThe predicate to test on every value of the range [stencil, stencil + (last - first)).
new_valueThe replacement value to assign when pred(*s) evaluates to true.
result + (last-first)
Template Parameters
InputIterator1is a model of Input Iterator.
InputIterator2is a model of Input Iterator and InputIterator2'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.
first may equal result, but the ranges [first, last) and [result, result + (last - first)) shall not overlap otherwise.
stencil may equal result, but the ranges [stencil, stencil + (last - first)) 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] = 10;
A[1] = 20;
A[2] = 30;
A[3] = 40;
S[0] = -1;
S[1] = 0;
S[2] = -1;
S[3] = 0;
is_less_than_zero pred;
thrust::replace_if(A.begin(), A.end(), S.begin(), B.begin(), pred, 0);
// B contains [0, 20, 0, 40]
See also