std::unique (3) - Linux Manuals
std::unique: std::unique
NAME
Synopsis
Defined in header <algorithm>
template< class ForwardIt > (until C++20)
ForwardIt unique( ForwardIt first, ForwardIt last );
template< class ForwardIt > (since C++20)
constexpr ForwardIt unique( ForwardIt first, ForwardIt last );
template< class ExecutionPolicy, class ForwardIt > (2) (since C++17)
ForwardIt unique( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last ); (1)
template< class ForwardIt, class BinaryPredicate > (until C++20)
ForwardIt unique( ForwardIt first, ForwardIt last, BinaryPredicate p );
template< class ForwardIt, class BinaryPredicate > (3) (since C++20)
constexpr ForwardIt unique( ForwardIt first, ForwardIt last, BinaryPredicate p );
template< class ExecutionPolicy, class ForwardIt, class BinaryPredicate > (4) (since C++17)
ForwardIt unique( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, BinaryPredicate p );
Eliminates all but the first element from every consecutive group of equivalent elements from the range [first, last) and returns a past-the-end iterator for the new logical end of the range.
Removing is done by shifting the elements in the range in such a way that elements to be erased are overwritten. Relative order of the elements that remain is preserved and the physical size of the container is unchanged. Iterators pointing to an element between the new logical end and the physical end of the range are still dereferenceable, but the elements themselves have unspecified values. A call to unique is typically followed by a call to a container's erase method, which erases the unspecified values and reduces the physical size of the container to match its new logical size.
1) Elements are compared using operator==. The behavior is undefined if it is not an equivalence_relation.
3) Elements are compared using the given binary predicate p. The behavior is undefined if it is not an equivalence relation.
2,4) Same as (1,3), but executed according to policy. These overloads do not participate in overload resolution unless std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true.
Parameters
first, last - the range of elements to process
policy - the execution policy to use. See execution_policy for details.
p - While the signature does not need to have const &, the function must not modify the objects passed to it and must be able to accept all values of type (possibly const) Type1 and Type2 regardless of value_category (thus, Type1 & is not allowed
Type requirements
-
ForwardIt must meet the requirements of LegacyForwardIterator.
-
The type of dereferenced ForwardIt must meet the requirements of MoveAssignable.
Return value
Forward iterator to the new end of the range
Complexity
For nonempty ranges, exactly std::distance(first,last) -1 applications of the corresponding predicate.
Exceptions
The overloads with a template parameter named ExecutionPolicy report errors as follows:
* If execution of a function invoked as part of the algorithm throws an exception and ExecutionPolicy is one of the standard_policies, std::terminate is called. For any other ExecutionPolicy, the behavior is implementation-defined.
* If the algorithm fails to allocate memory, std::bad_alloc is thrown.
Possible implementation
First version
Second version
Example
// Run this code