Thrust

◆ unique_copy() [3/4]

template<typename DerivedPolicy , typename InputIterator , typename OutputIterator , typename BinaryPredicate >
__host__ __device__ OutputIterator thrust::unique_copy ( const thrust::detail::execution_policy_base< DerivedPolicy > &  exec,
InputIterator  first,
InputIterator  last,
OutputIterator  result,
BinaryPredicate  binary_pred 
)

unique_copy copies elements from the range [first, last) to a range beginning with result, except that in a consecutive group of duplicate elements only the first one is copied. The return value is the end of the range to which the elements are copied.

This version of unique_copy uses the function object binary_pred to test for equality.

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

Parameters
execThe execution policy to use for parallelization.
firstThe beginning of the input range.
lastThe end of the input range.
resultThe beginning of the output range.
binary_predThe binary predicate used to determine equality.
Returns
The end of the unique range [result, result_end).
Template Parameters
DerivedPolicyThe name of the derived execution policy.
InputIteratoris a model of Input Iterator, and InputIterator's value_type is a model of Equality Comparable.
OutputIteratoris a model of Output Iterator and and InputIterator's value_type is convertible to OutputIterator's value_type.
BinaryPredicateis a model of Binary Predicate.
Precondition
The range [first,last) and the range [result, result + (last - first)) shall not overlap.

The following code snippet demonstrates how to use unique_copy to compact a sequence of numbers to remove consecutive duplicates using the thrust::host execution policy for parallelization:

#include <thrust/unique.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1};
int B[N];
int *result_end = thrust::unique_copy(thrust::host, A, A + N, B, thrust::equal_to<int>());
// The first four values of B are now {1, 3, 2, 1} and (result_end - B) is 4
// Values beyond result_end are unspecified.
See also
unique
https://en.cppreference.com/w/cpp/algorithm/unique_copy