📄 rem_0514.htm
字号:
<HTML><TITLE>remove_if</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_if</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></PRE>
<PRE>
template <class ForwardIterator, class Predicate>
ForwardIterator <B>remove_if</B> (ForwardIterator first,
ForwardIterator last,
Predicate pred);
</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P>The <B><I>remove_if</B></I> algorithm eliminates all the elements referred to by iterator <SAMP>i</SAMP> in the range <SAMP>[first, last)</SAMP> for which the following corresponding condition holds: <SAMP>pred(*i) == true</SAMP>. <B><I>remove_if</B></I> returns the end of the resulting range. <B><I>remove_if</B></I> is stable, that is, the relative order of the elements that are not removed is the same as their relative order in the original range. </P>
<P><B><I>remove_if</B></I> does not actually reduce the size of the sequence. It actually operates by: 1) copying the values that are to be <I>retained</I> to the front of the sequence, and 2) returning an iterator that describes where the sequence of retained values ends. Elements that are after this iterator are simply the original sequence values, left unchanged. Here's a simple example:</P>
<P>Say we want to remove all even numbers from the following sequence:</P>
<PRE> 123456789</PRE>
<PRE></PRE><P>Applying the <B><I>remove_if</B></I> algorithm results in the following sequence:</P>
<PRE> 13579|XXXX</PRE>
<P>The vertical bar represents the position of the iterator returned by <B><I>remove_if</B></I>. Note that the elements to the left of the vertical bar are the original sequence with the even numbers removed. The elements to the right of the bar are simply the untouched original members of the original sequence. </P>
<P>If you want to actually delete items from the container, use the following technique:</P>
<PRE>container.erase(remove(first,last,value),container.end());</PRE>
<PRE></PRE>
<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> </PRE><PRE> template<class Arg>
struct all_true : public unary_function<Arg, bool>
{
bool operator()(const Arg& x){ return 1; }
};</PRE><PRE> int main ()
{
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
vector<int> v(arr, arr+10);</PRE><PRE> copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;</PRE>
<PRE> // remove the 7
vector<int>::iterator result =
remove(v.begin(), v.end(), 7);
// delete dangling elements from the vector
v.erase(result, v.end());</PRE><PRE> copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;</PRE>
<PRE> // remove everything beyond the fourth element
<B> </B>result = <B>remove_if</B>(v.begin()+4,
v.begin()+8, all_true<int>());
// delete dangling elements
v.erase(result, v.end());</PRE><PRE> copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;</PRE>
<PRE> return 0;</PRE>
<PRE> }</PRE>
<PRE>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, 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>
<A NAME="See Also"><H3>See Also</H3></A>
<P><A HREF="rem_4572.htm"><B><I>remove</B></I></A>, <A HREF="rem_6253.htm"><B><I>remove_copy</B></I></A>, <A HREF="rem_9276.htm"><B><I>remove_copy_if</B></I></A></P>
<HR>
<A HREF="rem_9276.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="rep_6131.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -