HP C++ User Documentation


unique (3C++std) - Tru64 UNIX

Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.


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


  #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);


  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

    *i  ==  *(i  -  1)


  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.


  Exactly (last - first) - 1 applications of the corresponding predicate are


  // 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};
    vector<int> v(a1, a1+20), result;
     //Create an insert_iterator for results
    insert_iterator<vector<int> > ins(result,
     //Demonstrate includes
    cout << "The vector: " << endl << "    ";
    copy(v.begin(),v.end(),ostream_iterator<int,char>(cout," "));
     //Find the unique elements
     unique_copy(v.begin(), v.end(), ins);
     //Display the results
    cout << endl << endl
          << "Has the following unique elements:"
          << endl << "     ";
         ostream_iterator<int,char>(cout," "));
    return 0;
  Output :
  The vector:
     4 5 5 9 -1 -1 -1 3 7 5 5 5 6 7 7 7 4 2 1 1
  Has the following unique elements:
      4 5 9 -1 3 7 5 6 7 4 2 1


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

  vector<int, allocator<int> >

  instead of:



  ANSI X3J16/ISO WG21 Joint C++ Committee
About PDF files: The PDF files on this site can be read online or printed using Adobe® Acrobat® Reader. If you do not have this software on your system, you may download it from Adobe's website.
Privacy statement Using this site means you accept its terms C++ support
© 2008 Hewlett-Packard Development Company, L.P.