Thrust

◆ unique() [3/4]

template<typename DerivedPolicy , typename ForwardIterator , typename BinaryPredicate >
__host__ __device__ ForwardIterator thrust::unique ( const thrust::detail::execution_policy_base< DerivedPolicy > &  exec,
ForwardIterator  first,
ForwardIterator  last,
BinaryPredicate  binary_pred 
)

For each group of consecutive elements in the range [first, last) with the same value, unique removes all but the first element of the group. The return value is an iterator new_last such that no two consecutive elements in the range [first, new_last) are equal. The iterators in the range [new_last, last) are all still dereferenceable, but the elements that they point to are unspecified. unique is stable, meaning that the relative order of elements that are not removed is unchanged.

This version of unique 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.
binary_predThe binary predicate used to determine equality.
Returns
The end of the unique range [first, new_last)
Template Parameters
DerivedPolicyThe name of the derived execution policy.
ForwardIteratoris a model of Forward Iterator, and ForwardIterator is mutable, and ForwardIterator's value_type is convertible to BinaryPredicate's first_argument_type and to BinaryPredicate's second_argument_type.
BinaryPredicateis a model of Binary Predicate.

The following code snippet demonstrates how to use unique 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 *new_end = thrust::unique(thrust::host, A, A + N, thrust::equal_to<int>());
// The first four values of A are now {1, 3, 2, 1}
// Values beyond new_end are unspecified.
See also
https://en.cppreference.com/w/cpp/algorithm/unique
unique_copy