📄 rem_6253.htm
字号:
<HTML><TITLE>remove_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>remove_copy</H2>
<HR><PRE> Algorithm</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>Move desired elements to the front of a container, and return an iterator that describes where the sequence of desired elements ends.</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>
<A HREF="#See Also"><LI>See Also</LI></A>
</UL>
<A NAME="Synopsis"><H3>Synopsis</H3></A>
<PRE>#include <algorithm>
template <class InputIterator,
class OutputIterator,
class T>
OutputIterator <B>remove_copy</B> (InputIterator first,
InputIterator last,
OutputIterator result,
const T& value);
</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P>The <B><I>remove_copy</B></I> algorithm copies all the elements referred to by the iterator <SAMP>i</SAMP> in the range <SAMP>[first, last)</SAMP> for which the following corresponding condition does <I>not</I> hold: <SAMP> *i == value</SAMP>. <B><I>remove_copy</B></I> returns the end of the resulting range. <B><I>remove_copy</B></I> is stable, that is, the relative order of the elements in the resulting range is the same as their relative order in the original range. The elements in the original sequence are not altered by <B><I>remove_copy</B></I>.</P>
<A NAME="Complexity"><H3>Complexity</H3></A>
<P>Exactly <SAMP>last1 - first1 </SAMP>applications of the corresponding predicate are done. </P>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// remove.cpp
//
#include <algorithm>
#include <vector>
#include <iterator>
#include <iostream.h>
template<class Arg>
struct all_true : public unary_function<Arg, bool>
{
bool operator() (const Arg&) { return 1; }
};
int main ()
{
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
vector<int> v(arr+0, arr+10);
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
//
// Remove the 7.
//
vector<int>::iterator result = remove(v.begin(), v.end(), 7);
//
// Delete dangling elements from the vector.
//
v.erase(result, v.end());
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
//
// Remove everything beyond the fourth element.
//
result = remove_if(v.begin()+4, v.begin()+8, all_true<int>());
//
// Delete dangling elements.
//
v.erase(result, v.end());
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
//
<B> </B>// Now remove all 3s on output.
//
<B>remove_copy</B>(v.begin(), v.end(), ostream_iterator<int>(cout," "), 3);</PRE>
<PRE> cout << endl << endl;
//
// Now remove everything satisfying predicate on output.
// Should yield a NULL vector.
//
remove_copy_if(v.begin(), v.end(), ostream_iterator<int>(cout," "),</PRE>
<PRE> all_true<int>());
return 0;
}
Output :
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 8 9 10
1 2 3 4
1 2 4</PRE>
<A NAME="Warning"><H3>Warning</H3></A>
<P>If your compiler does not support default template parameters, 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>
<A NAME="See Also"><H3>See Also</H3></A>
<P><A HREF="rem_4572.htm"><B><I>remove</B></I></A>, <A HREF="rem_0514.htm"><B><I>remove_if</B></I></A>, <A HREF="rem_9276.htm"><B><I>remove_copy_if</B></I></A></P>
<HR>
<A HREF="rem_4572.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="rem_9276.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -