那个蛋痛的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报的错真的要折腾半天啊.....

转载于:https://www.cnblogs.com/vimmer/archive/2012/10/27/2743045.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值