Standard C++ Library Class Reference
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 tests
for equality, and the other tests whether a binary predicate applied to adjacent elements is true.
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
Exactly (last - first) - 1 applications of the corresponding predicate are performed.
Example
//
// unique.cpp
//
#include <algorithm>
#include <vector>
#include <iostream.h>
int main()
{
//Initialize two vectors
int a1[20] = {4, 5, 5, 9, -1, -1, -1, 3, 7, 5,
5, 5, 6, 7, 7, 7, 4, 2, 1, 1};