• 那个蛋痛的list的remove_if中用到的对像函数


    题目够长的....

    其实有时.要小用list来作一些过滤..我知道list的随机查找不太好...可是有时写好了,,性能上也没有太多要求..就....

    所以我就直接remove_if...

    先在 

    http://www.cplusplus.com/reference/stl/list/remove_if/

    找了个示例

    代码如下

    // list::remove_if
    #include <iostream>
    #include <list>
    using namespace std;
    
    // a predicate implemented as a function:
    bool single_digit (const int& value) { return (value<10); }
    
    // a predicate implemented as a class:
    class is_odd
    {
    public:
      bool operator() (const int& value) {return (value%2)==1; }
    };
    
    int main ()
    {
      int myints[]= {15,36,7,17,20,39,4,1};
      list<int> mylist (myints,myints+8);   // 15 36 7 17 20 39 4 1
    
      mylist.remove_if (single_digit);      // 15 36 17 20 39
    
      mylist.remove_if (is_odd());          // 36 20
    
      cout << "mylist contains:";
      for (list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
        cout << " " << *it;
      cout << endl;
    
      return 0;
    }

    但发现所接的有时需要在 remove_if所接的 函数中加入传入参数等....

    好像直接用函数会出错.

    但观察得.

    // a predicate implemented as a class:
    class is_odd
    {
    public:
      bool operator() (const int& value) {return (value%2)==1; }
    };
    所以就直接自己再加了一个测试...

    class less_than                                                             
    {                                                                           
            public:                                                             
                    less_than(int mid):_mid(mid){};                             
                    bool operator()(const int& value) {return value < _mid;}    
                    int _mid;                                                   
    };                                                                          

    在调用的main中加入

    
    
     
    int main ()
    {
      int myints[]= {15,36,7,17,20,39,4,1};
      list<int> mylist (myints,myints+8);   // 15 36 7 17 20 39 4 1
    
      mylist.remove_if (single_digit);      // 15 36 17 20 39
    
      mylist.remove_if (is_odd());          // 36 20
    
    
       //invoke here begin 
      mylist.remove_if(less_than(30));        //36  
       //invoke here end
      cout << "mylist contains:";
      for (list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
        cout << " " << *it;
      cout << endl;
    
      return 0;
    }

    但这里list所放的是int这种基本元素..我试着放入自定义的数据类型的时候就出问题了

    如加入这个数据类型

                                                           
     class Cmd                                             
     {                                                     
             public:                                       
                                                           
                     Cmd(string name):_name(name){};       
                     string GetName(){return _name;}       
                     string _name;                         
     };                                                    

    在main中加入

                                                           
    Cmd* c1 = new Cmd("c1");                               
    Cmd* c2 = new Cmd("c2");                               
    list<Cmd*> cmd_list;                                   
    cmd_list.push_back(c1);                                
    cmd_list.push_back(c2);                                
    cout << "size = [" << cmd_list.size() <<endl;          
                                                           
                                  
    string str = "c1";                                     
     
                                                           
    cmd_list.remove_if(lt2(str));    //compile err this line
    
                           
    cout << "size = [" << cmd_list.size() <<endl;          
    delete c1;                                             
    delete c2;       

    其中lt2的定义如下

    class lt2
    {                                                       
            public:                                         
                    lt2(string name) :_name(name){};        
                    bool operator()( const Cmd*& cmd) 
                    {                                       
                            return cmd->GetName() == _name; 
                    }                                       
            private:                                        
                    string _name;                           
    };                                                      

    后来参考了

    http://blog.csdn.net/lonelysky/article/details/6584303

    这篇文章,发现如果非基楚数据类型要继承某个类..

    所以改成了

    class lt2 : public unary_function<Cmd*, bool>             
    {                                                         
            public:                                           
                    lt2(string name) :_name(name){};   
                  //  bool operator()(const  Cmd*& cmd) 
                    bool operator()( Cmd*& cmd) const         
                    {                                         
                            return cmd->GetName() == _name;   
                    }                                         
            private:                                          
                    string _name;                             
    };                                                        

    就可以了.....

    看来有时遇到STL报的错真的要折腾半天啊.....

  • 相关阅读:
    欧几里得算法及扩展欧几里得(含)
    RP
    P1734_最大约数和
    The 2017 ACM-ICPC Asia East Continent League Final记录
    【数据结构】bzoj1651专用牛棚
    【数据结构】bzoj1455罗马游戏
    【数据结构】bzoj1636/bzoj1699排队
    【数据结构】bzoj3747Kinoman
    【计算几何】奇特的门
    Topcoder SRM 608 div1 题解
  • 原文地址:https://www.cnblogs.com/vimmer/p/2743045.html
Copyright © 2020-2023  润新知