📄 uni_7998.htm
字号:
<HTML><TITLE>unique, unique_copy</TITLE><BODY>
<A HREF="ref.htm"><IMG SRC="images/banner.gif"></A>
<P><STRONG>Click on the banner to return to the Class Reference home page.</STRONG></P>
<P>©Copyright 1996 Rogue Wave Software</P>
<H2>unique, unique_copy</H2>
<HR><PRE> Algorithm</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>Removes consecutive duplicates from a range of values and places the resulting unique values into the result.</P>
<H3>Contents</H3>
<UL>
<A HREF="#Synopsis"><LI>Synopsis</LI></A>
<A HREF="#Description"><LI>Description</LI></A>
<A HREF="#Complexity"><LI>Complexity</LI></A>
<A HREF="#Example"><LI>Example</LI></A>
<A HREF="#Warning"><LI>Warning</LI></A>
</UL>
<A NAME="Synopsis"><H3>Synopsis</H3></A>
<PRE>#include <algorithm></PRE>
<PRE>
template <class ForwardIterator>
ForwardIterator <B>unique</B> (ForwardIterator first,
ForwardIterator last);
template <class ForwardIterator, class BinaryPredicate>
ForwardIterator <B>unique</B> (ForwardIterator first,
ForwardIterator last,
BinaryPredicate binary_pred);
template <class InputIterator, class OutputIterator>
OutputIterator <B>unique_copy</B> (InputIterator first,
InputIterator last,
OutputIterator result);
template <class InputIterator,
class OutputIterator,
class BinaryPredicate>
OutputIterator <B>unique_copy</B> (InputIterator first,
InputIterator last,
OutputIterator result,
BinaryPredicate binary_pred);
</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P>The <B><I>unique</B></I> 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:</P>
<PRE> *i == *(i - 1) </PRE>
<PRE></PRE><P>or </P>
<PRE>binary_pred(*i, *(i - 1)) == true. </PRE>
<PRE></PRE><P>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 <B><I>unique</B></I> returns the end of the resulting range. </P>
<P>The <A HREF="uni_7998.htm"><B><I>unique_copy</B></I></A> algorithm copies the first element from every consecutive group of equal elements, to an OutputIterator. The <B><I>unique_copy</B></I> algorithm, also has two versions--one that tests for equality and a second that tests adjacent elements against a binary predicate.</P>
<P><A HREF="uni_7998.htm"><B><I>unique_copy</B></I></A> returns the end of the resulting range. </P>
<A NAME="Complexity"><H3>Complexity</H3></A>
<P>Exactly <SAMP>(last - first) - 1</SAMP> applications of the corresponding predicate are performed.</P>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// 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;</PRE><PRE> //Create an insert_iterator for results
insert_iterator<vector<int> > ins(result,
result.begin());</PRE><PRE> //Demonstrate includes
cout << "The vector: " << endl << " ";
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));</PRE><PRE> //Find the unique elements
<B>unique_copy(</B>v.begin(), v.end(), ins);</PRE>
<PRE> //Display the results
cout << endl << endl
<< "Has the following unique elements:"
<< endl << " ";
copy(result.begin(),result.end(),
ostream_iterator<int>(cout," "));</PRE><PRE> return 0;
}</PRE>
<PRE>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
</PRE>
<A NAME="Warning"><H3>Warning</H3></A>
<P>If your compiler does not support default template parameters, then you need to always supply the <SAMP>Allocator</SAMP> template argument. For instance, you will need to write : </P>
<P><SAMP>vector<int, allocator></SAMP></P>
<P>instead of:</P>
<P><SAMP>vector<int></SAMP></P>
<HR>
<A HREF="uni_6183.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="upp_0967.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -