zoukankan      html  css  js  c++  java
  • 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 ints
    int myInts[]={1,7,8,4,5,10,15,22,50.29};
    std
    ::vector v(myInts, myInts +sizeof(myInts)/sizeof(int));

    // get rid of anything < 10
    std
    ::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
  • 相关阅读:
    shell---telnet shell实现
    设计模式-建造者模式
    关于Http2
    转载Resharper使用
    设计模式-原型模式
    设计模式-代理模式
    设计模式-装饰器模式
    设计模式-简单工厂和策略模式
    C#直接发送打印机命令到打印机及ZPL常用打印命令
    C#打印机操作类
  • 原文地址:https://www.cnblogs.com/youxin/p/2558394.html
Copyright © 2011-2022 走看看