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使用


2
[down vote](down vote)
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 < 10
v.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);

输出:10 15 22 50 5 10 15 22 50

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;
    }

输出:0 1 2 3 4 7 8 9 9 9

可以看到,remove后vector的size并没有变化

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

重复对你有好处:

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

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

上面解释了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

原文链接: https://www.cnblogs.com/youxin/archive/2012/06/22/2558394.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/53269

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 上午4:38
下一篇 2023年2月9日 上午4:40

相关推荐