replace_copy_if#

Overloads#

replace_copy_if(exec, first, last, result, pred, new_value)#

template<typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename Predicate, typename T>
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.

#include <thrust/replace.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.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;

thrust::device_vector<int> B(4);
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

replace

See also

replace_if

See also

replace_copy

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence to copy from.

  • last – The end of the sequence to copy from.

  • result – The beginning of the sequence to copy to.

  • pred – The predicate to test on every value of the range [first,last).

  • new_value – The replacement value to assign pred(*i) evaluates to true.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument type.

  • OutputIterator – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

  • T – is a model of Assignable, and T is convertible to OutputIterator's value_type.

Returns:

result + (last-first)

Pre:

first may equal result, but the ranges [first, last) and [result, result + (last - first)) shall not overlap otherwise.

replace_copy_if(first, last, result, pred, new_value)#

template<typename InputIterator, typename OutputIterator, typename Predicate, typename T>
OutputIterator thrust::replace_copy_if(
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.

#include <thrust/replace.h>
#include <thrust/device_vector.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;

thrust::device_vector<int> B(4);
is_less_than_zero pred;

thrust::replace_copy_if(A.begin(), A.end(), B.begin(), pred, 0);

// B contains [1, 0, 2, 0]

See also

replace

See also

replace_if

See also

replace_copy

Parameters:
  • first – The beginning of the sequence to copy from.

  • last – The end of the sequence to copy from.

  • result – The beginning of the sequence to copy to.

  • pred – The predicate to test on every value of the range [first,last).

  • new_value – The replacement value to assign pred(*i) evaluates to true.

Template Parameters:
  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument type.

  • OutputIterator – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

  • T – is a model of Assignable, and T is convertible to OutputIterator's value_type.

Returns:

result + (last-first)

Pre:

first may equal result, but the ranges [first, last) and [result, result + (last - first)) shall not overlap otherwise.

replace_copy_if(exec, first, last, stencil, result, pred, new_value)#

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator, typename Predicate, typename T>
OutputIterator thrust::replace_copy_if(
const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
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.

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

#include <thrust/replace.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.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;

thrust::device_vector<int> S(4);
S[0] = -1;
S[1] =  0;
S[2] = -1;
S[3] =  0;

thrust::device_vector<int> B(4);
is_less_than_zero pred;

thrust::replace_if(thrust::device, A.begin(), A.end(), S.begin(), B.begin(), pred, 0);

// B contains [0, 20, 0, 40]

See also

replace_copy

See also

replace_if

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the sequence to copy from.

  • last – The end of the sequence to copy from.

  • stencil – The beginning of the stencil sequence.

  • result – The beginning of the sequence to copy to.

  • pred – The predicate to test on every value of the range [stencil, stencil + (last - first)).

  • new_value – The replacement value to assign when pred(*s) evaluates to true.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator.

  • InputIterator2 – is a model of Input Iterator and InputIterator2's value_type is convertible to Predicate's argument type.

  • OutputIterator – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

  • T – is a model of Assignable, and T is convertible to OutputIterator's value_type.

Returns:

result + (last-first)

Pre:

first may equal result, but the ranges [first, last) and [result, result + (last - first)) shall not overlap otherwise.

Pre:

stencil may equal result, but the ranges [stencil, stencil + (last - first)) and [result, result + (last - first)) shall not overlap otherwise.

replace_copy_if(first, last, stencil, result, pred, new_value)#

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.

#include <thrust/replace.h>
#include <thrust/device_vector.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;

thrust::device_vector<int> S(4);
S[0] = -1;
S[1] =  0;
S[2] = -1;
S[3] =  0;

thrust::device_vector<int> B(4);
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

replace_copy

See also

replace_if

Parameters:
  • first – The beginning of the sequence to copy from.

  • last – The end of the sequence to copy from.

  • stencil – The beginning of the stencil sequence.

  • result – The beginning of the sequence to copy to.

  • pred – The predicate to test on every value of the range [stencil, stencil + (last - first)).

  • new_value – The replacement value to assign when pred(*s) evaluates to true.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator.

  • InputIterator2 – is a model of Input Iterator and InputIterator2's value_type is convertible to Predicate's argument type.

  • OutputIterator – is a model of Output Iterator.

  • Predicate – is a model of Predicate.

  • T – is a model of Assignable, and T is convertible to OutputIterator's value_type.

Returns:

result + (last-first)

Pre:

first may equal result, but the ranges [first, last) and [result, result + (last - first)) shall not overlap otherwise.

Pre:

stencil may equal result, but the ranges [stencil, stencil + (last - first)) and [result, result + (last - first)) shall not overlap otherwise.