◆ replace_if() [2/4]

template<typename ForwardIterator , typename Predicate , typename T >
void thrust::replace_if ( ForwardIterator  first,
ForwardIterator  last,
Predicate  pred,
const T &  new_value 

replace_if replaces every element in the range [first, last) for which pred returns true with new_value. That is: for every iterator i, if pred(*i) is true then it performs the assignment *i = new_value.

firstThe beginning of the sequence of interest.
lastThe end of the sequence of interest.
predThe predicate to test on every value of the range [first,last).
new_valueThe new value to replace elements which pred(*i) evaluates to true.
Template Parameters
ForwardIteratoris a model of Forward Iterator, ForwardIterator is mutable, and ForwardIterator's value_type is convertible to Predicate's argument_type.
Predicateis a model of Predicate.
Tis a model of Assignable, and T is convertible to ForwardIterator's value_type.

The following code snippet demonstrates how to use replace_if to replace a device_vector's negative elements with 0.

#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_if(A.begin(), A.end(), pred, 0);
// A contains [1, 0, 2, 0]
See also