### 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`