Rogue Wave banner
Previous fileTop of documentContentsIndexNext file

unique, unique_copy


Algorithm

Summary

Removes consecutive duplicates from a range of values and places the resulting unique values into the result.

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

None

Synopsis

#include <algorithm>
template <class ForwardIterator>
  ForwardIterator
    unique (ForwardIterator first, ForwardIterator last);
template <class ForwardIterator, class BinaryPredicate>
  ForwardIterator
    unique (ForwardIterator first, ForwardIterator last,
            BinaryPredicate binary_pred);
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);

Description

The unique algorithm moves through a sequence and eliminates all but the first element from every consecutive group of equal elements. There are two versions of the algorithm-one that tests for equality and a second that tests adjacent elements against a binary predicate. An element is unique if it does not meet the corresponding condition listed here:

*i == *(i - 1)

or

binary_pred(*i, *(i - 1)) == true.

If an element is unique, it is copied to the front of the sequence, overwriting the existing elements. Once all unique elements have been identified. The remainder of the sequence is left unchanged, and unique returns the end of the resulting range.

The unique_copy algorithm copies the first element from every consecutive group of equal elements to an OutputIterator. The unique_copy algorithm also has two versions-one that tests for equality and a second that tests adjacent elements against a binary predicate.

unique_copy returns the end of the resulting range.

Complexity

For unique_copy, it is exactly (last - first) - 1 applications of the corresponding predicate are performed.

Example

Program Output

Warnings

If your compiler does not support default template parameters, then you always need to supply the Allocator template argument. For instance, you need to write:

vector<int, allocator<int> >

instead of:

vector<int>

If your compiler does not support namespaces, then you do not need the using declaration for std.



Previous fileTop of documentContentsIndexNext file
©Copyright 1998, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.
OEM Release, June 1998