#include <algorithm> template< class ForwardIterator, class OutputIterator > ForwardIterator unique_copy( ForwardIterator first, ForwardIterator last, OutputIterator d_first ); template< class ForwardIterator, class OutputIterator, class BinaryPredicate > ForwardIterator unique_copy( ForwardIterator first, ForwardIterator last, OutputIterator d_first, BinaryPredicate p );
Copies the elements from the range [first, last)
, to another range beginning at d_first
in such a way, that there are no consecutive equal elements. Only the first element of each group of equal elements is copied. One version of the function uses operator==
to compare the elements, another uses the given binary predicate p
.
first
, last
- the range of the elements to be processed
d_first
- the beginning of the destination range
p
- binary predicate which returns true
if the elements should be treated as equal
An iterator to the end of the new range.
First version:
template<class ForwardIterator, class OutputIterator> ForwardIterator unique_copy(ForwardIterator first, ForwardIterator last, OutputIterator d_first) { *d_first=*first; while (++first != last) { if (!(*d_first == *first)) { // or: if (!pred(*result,*first)) for the pred version *(++d_first) = *first; } } return ++d_first; }
Second version:
template<class ForwardIterator, class OutputIterator, class BinaryPredicate> ForwardIterator unique_copy(ForwardIterator first, ForwardIterator last, OutputIterator d_first, BinaryPredicate p) { *d_first=*first; while (++first != last) { if (!p(*result, *first)) { *(++d_first) = *first; } } return ++d_first; }
linear in distance between first
and last