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 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. |
- Returns
- The end of the unique range
[first, new_last)
.
- Template Parameters
-
DerivedPolicy | The name of the derived execution policy. |
ForwardIterator | is a model of Forward Iterator, and ForwardIterator is mutable, and ForwardIterator's value_type is a model of Equality Comparable. |
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:
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1};
- See also
- https://en.cppreference.com/w/cpp/algorithm/unique
-
unique_copy