.\" ident @(#)unique.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH unique 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2unique\fP, \f2unique_copy\fP \ - Removes consecutive duplicates from a range of values and places the resulting unique values into the result. .SH SYNOPSIS .RE .RS 0 #include .br template .RE .RS 1 ForwardIterator .RE .RS 4 unique (ForwardIterator first, ForwardIterator last); .RE .RS 0 template .RE .RS 1 ForwardIterator .RE .RS 4 unique (ForwardIterator first, ForwardIterator last, .RE .RS 11 BinaryPredicate binary_pred); .RE .RS 0 template .RE .RS 1 OutputIterator .RE .RS 4 unique_copy (InputIterator first, InputIterator last, .RE .RS 16 OutputIterator result); .RE .RS 0 template .RE .RS 1 OutputIterator .RE .RS 4 unique_copy (InputIterator first, InputIterator last, .RE .RS 16 OutputIterator result, .br BinaryPredicate binary_pred); .SH 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: \f2*i == *(i - 1) \fP or \f2binary_pred(*i, *(i - 1)) == true. \fP 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. .SH COMPLEXITY For unique_copy, it is exactly \f2(last - first) - 1\fP applications of the corresponding predicate are performed. .SH EXAMPLE .RE .RS 0 // .br // unique.cpp .br // .br #include .br #include .br #include .br using namespace std; .br int main() .RE .RS 1 { .RE .RS 0 //Initialize two vectors .br int a1[20] = {4, 5, 5, 9, -1, -1, -1, 3, 7, 5, .RE .RS 13 5, 5, 6, 7, 7, 7, 4, 2, 1, 1}; .RE .RS 0 vector v(a1+0, a1+20), result; .br .br //Create an insert_iterator for results .br insert_iterator > ins(result, result.begin()); .br .br //Demonstrate includes .br cout << "The vector: " << endl << " "; .br copy(v.begin(),v.end(), .RE .RS 4 ostream_iterator(cout," ")); .RE .RS 0 .br //Find the unique elements .br unique_copy(v.begin(), v.end(), ins); .br .br //Display the results .br cout << endl << endl .RE .RS 5 << "Has the following unique elements:" .br << endl << " "; .RE .RS 0 copy(result.begin(),result.end(), .RE .RS 4 ostream_iterator(cout," ")); .RE .RS 0 return 0; .br } .br .RE .RS 0 Program Output .br .br The vector: .br 4 5 5 9 -1 -1 -1 3 7 5 5 5 6 7 7 7 4 2 1 1 .br Has the following unique elements: .br 4 5 9 -1 3 7 5 6 7 4 2 1 .SH WARNINGS If your compiler does not support default template parameters, then you always need to supply the \f2Allocator\fP template argument. For instance, you need to write: \f2vector >\fP instead of: \f2vector\fP If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP.