unique_copy
Copies elements while removing adjacent duplicates
You're viewing an older version of this page (#4149). View the current version.
Definition
The unique_copy() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.
Interface
#include <algorithm>
template < class InputIterator, class OutputIterator >
OutputIterator unique_copy(
InputIterator first,
InputIterator last,
OutputIterator result
);
template < class InputIterator, class OutputIterator, class BinaryPredicate >
OutputIterator unique_copy(
InputIterator first,
InputIterator last,
OutputIterator result,
BinaryPredicate comp,
);Parameters:
| Parameter | Description |
| first | A forward iterator addressing the position of the first element in the source range to be copied |
| last | A forward iterator addressing the position one past the final element in the source range to be copied |
| result | An output iterator addressing the position of the first element in the destination range that is receiving the copy with consecutive duplicates removed |
| 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_copy algorithm copies a sequence such as each duplicate consecutive elements become an unique element.
The first version uses operator== to compare the elements, the second version uses the given binary predicate comp.
Return Value
Returns an iterator pointing to the end of the output range.
Complexity
The complexity is linear; performs (last - first) applications of operator== (for the first version) or of comp (for the second version) and at most last - first assignments.