unique
Removes adjacent duplicates (elements that are equal to their predecessor)
Definition
The unique() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.
Interface
#include <algorithm>
template < class ForwardIterator >
ForwardIterator unique(
ForwardIterator first,
ForwardIterator last
);
template < class ForwardIterator, class Predicate >
ForwardIterator unique(
ForwardIterator first,
ForwardIterator last,
Predicate comp
);Parameters:
| Parameter | Description |
| first | A forward iterator addressing the position of the first element in the range to be scanned for duplicate removal |
| last | A forward iterator addressing the position one past the final element in the range to be scanned for duplicate removal |
| comp | User-defined predicate function object that defines the condition to be satisfied if two elements are to be taken as equivalent. A binary predicate takes two arguments and returns true when satisfied and false when not satisfied |
Description
Unique algorithm transforms a sequence such as each duplicate consecutive elements become a unique element.
The first version uses operator== to compare the elements, the second version uses the given binary predicate comp.
Return Value
The return value is an iterator pointing to the end of the collapsed range.
Complexity
The complexity is linear; performs (last - first) - 1 applications of operator== (for the first version) or of comp (for the second version).