Namespaces
Variants
Views
Actions

std::unique_copy

From cppreference.com
< cpp‎ | algorithm
Revision as of 17:55, 17 August 2011 by Cubbi (Talk | contribs)

Template:cpp/algorithm/sidebar Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <algorithm>
</td>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td >
template< class ForwardIterator, class OutputIterator >

ForwardIterator unique_copy( ForwardIterator first, ForwardIterator last,

                             OutputIterator d_first );
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class ForwardIterator, class OutputIterator, class BinaryPredicate >

ForwardIterator unique_copy( ForwardIterator first, ForwardIterator last,

                             OutputIterator d_first, BinaryPredicate p );
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

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. The first version uses operator== to compare the elements, the second version uses the given binary predicate p.

Contents

Parameters

first, last - the range of elements to process
d_first - the beginning of the destination range
p - binary predicate which returns ​true if the elements should be treated as equal.

The signature of the predicate function should be equivalent to the following:

 bool pred(const Type1 &a, const Type2 &b);

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, nor is Type1 unless for Type1 a move is equivalent to a copy(since C++11)).
The types Type1 and Type2 must be such that an object of type ForwardIterator can be dereferenced and then implicitly converted to both of them. ​

Return value

output iterator to the element past the last written element

Equivalent function

Template:eq fun cpp

Example

Template:example cpp

Complexity

linear in the distance between first and last

See also

Template:cpp/algorithm/dcl list adjacent findTemplate:cpp/algorithm/dcl list unique