### c++ 模板函数remove（）

`template < class ForwardIterator, class T >  ForwardIterator remove ( ForwardIterator first, ForwardIterator last,                           const T& value );注意返回迭代器。`
Remove value from range
`Compares the elements in the range [first,last) against value, and removes those that compare equal from the resulting range. The resulting range consists of the elements between first and the iterator returned by the function, which points to the new end of the range.The relative order of the elements not removed is preserved, while the elements past the new end of range are still valid, although with unspecified values.通过上面的说明，可知从结果序列中remove相等的值，返回指向结果序列的末尾迭代器。先前的没有移除元素的序列保存。`
`#include <iostream>#include <algorithm>using namespace std;int main () {  int myints[] = {10,20,30,30,20,10,10,20};      // 10 20 30 30 20 10 10 20  // bounds of range:  int* pbegin = myints;                          // ^  int* pend = myints+sizeof(myints)/sizeof(int); // ^                       ^  pend = remove (pbegin, pend, 20);              // 10 30 30 10 10 ?  ?  ?                                                 // ^              ^  cout << "range contains:";  for (int* p=pbegin; p!=pend; ++p)    cout << " " << *p;  cout << endl;  for(int *lp=myints;lp!=myints+sizeof(myints)/sizeof(int);lp++)  //输出10 30 30 10 10    10 10 20     cout<<*lp<<ends;  cout<<endl;  for(int i=0;i<8;i++)      cout<<myints[i]<<ends; //输出10 30 30 10 10    10 10 20  cout<<endl;     return 0;}`

`至于为什么`
`for(int *lp=myints;lp!=myints+sizeof(myints)/sizeof(int);lp++)  后输出10 30 30 10 10    10 10 20深入看：http://www.dreamincode.net/forums/topic/32349-stl-remove-algorithm-c/erase（）结合remove_if使用`
 The most readable way I've done this in the past is to use `std::erase` combined with `std::remove_if`. In the example below, I use this combination to remove any number less than 10 from a vector. (For non-c++0x, you can just replace the lambda below with your own predicate:) ``// a list of intsint myInts[]={1,7,8,4,5,10,15,22,50.29};std::vector v(myInts, myInts +sizeof(myInts)/sizeof(int));// get rid of anything < 10std::erase(std::remove_if(v.begin(), v.end(),                           [](int i){return i <10;}), v.end());``
`#include <iostream>#include<vector>#include <algorithm>using namespace std;bool isP(int i){  return i<10;}int main () {int myInts[]={1,7,8,4,5,10,15,22,50.29};vector<int> v(myInts, myInts +sizeof(myInts)/sizeof(int));// get rid of anything < 10v.erase(remove_if(v.begin(), v.end(),                      isP), v.end());for(int i=0;i!=v.size();i++)cout<<v[i]<<ends;cout<<endl;   return 0;}`

如果只是：

remove_if(v.begin(), v.end(), isP);

`    vector<int> v;         int i;    for( i=0;i<10;i++)        v.push_back(i);         remove(v.begin(),v.end(),5);    remove(v.begin(),v.end(),6);    for(vector<int>::iterator it=v.begin();it!=v.end();it++)    {                 cout<<*it<<ends;    }`

remove并不“真的”删除东西，因为它做不到。

remove并不“真的”删除东西，因为它做不到

remove不知道它要从哪个容器删除东西，而没有容器，它就没有办法调用成员函数，而如果“真的”要删除东西，那就是必要的。

`上面的程序要这么该才正确：`
`vector<int>::iterator it2=remove(v.begin(),v.end(),5);    vector<int>::iterator newEnd=remove(v.begin(),it2,6);    for(vector<int>::iterator it=v.begin();it!=newEnd;it++)    {                 cout<<*it<<ends;    }`

`参考：http://stackoverflow.com/questions/4713131/removing-item-from-vector-while-iterating`