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.
The reason there are two different versions of unique_copy is that there are two different definitions of what it means for a consecutive group of elements to be duplicates. In the first version, the test is simple equality: the elements in a range [f, l)
are duplicates if, for every iterator i
in the range, either i == f
or else *i == *(i-1)
. In the second, the test is an arbitrary BinaryPredicate
binary_pred:
the elements in [f, l)
are duplicates if, for every iterator i
in the range, either i == f
or else binary_pred(*i, *(i-1))
is true
.
This version of unique_copy
uses operator==
to test for equality.
The algorithm's execution is parallelized as determined by exec
.
- Parameters
-
exec | The execution policy to use for parallelization. |
first | The beginning of the input range. |
last | The end of the input range. |
result | The beginning of the output range. |
- Returns
- The end of the unique range
[result, result_end)
.
- Template Parameters
-
DerivedPolicy | The name of the derived execution policy. |
InputIterator | is a model of Input Iterator, and InputIterator's value_type is a model of Equality Comparable. |
OutputIterator | is a model of Output Iterator and and InputIterator's value_type is convertible to OutputIterator's value_type . |
- 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:
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1};
int B[N];
- See also
- unique
-
https://en.cppreference.com/w/cpp/algorithm/unique_copy