unique_copy
PrototypeUnique_copy is an overloaded name; there are actually two unique_copy functions. 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 binary_pred); DescriptionUnique_copy copies elements from the range [first, last) to a range beginning with result, except that in a consecutive group of duplicate elements only the first one is copied. The return value is the end of the range to which the elements are copied. This behavior is similar to the Unix filter uniq.
The reason there are two different versions of DefinitionDefined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.Requirements on typesFor the first version:
Preconditions
ComplexityLinear. Exactlylast - first applications of operator== (in the case of the first version of unique) or of binary_pred (in the case of the second version), and at most last - first assignments. ExamplePrint all of the numbers in an array, but only print the first one in a consecutive group of identical numbers.const int A[] = {2, 7, 7, 7, 1, 1, 8, 8, 8, 2, 8, 8}; unique_copy(A, A + sizeof(A) / sizeof(int), ostream_iterator<int>(cout, " ")); // The output is "2 7 1 8 2 8". Notes[1] Strictly speaking, the first version ofunique_copy is redundant: you can achieve the same functionality by using an object of class equal_to as the BinaryPredicate argument. The first version is provided strictly for the sake of convenience: testing for equality is an important special case.
[2] See alsoBinaryPredicate,unique, remove_copy, remove_copy_if, adjacent_find | |||||||

