Translations of this page?:

unique_copy

#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.

Parameters

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

Return value

An iterator to the end of the new range.

Equivalent function

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;
}

Complexity

linear in distance between first and last

See also

 
• • • SitemapRecent changesRSScc