(转)STL中的适配器

来源:互联网 发布:大数据 意义 编辑:程序博客网 时间:2024/05/10 16:32

   我们知道在STL中函数对象发挥着很大作用:
     find_if(coll.begin(),coll.end(),bind2nd(greater<int>(),42));
     这里bind2nd就是一个函数对象,他提供了operator()的处理,是的我们可以象调用函数一样操作,这也就是他名字的由来.

     find_if的第三个参数我们也可以使用传递函数指针,比如
     bool mygreater(int t)
     {
        return true;
     }
     find_if(coll.begin(),coll.end(),mygreater);这样是完全合法的.
    

    但是问题来了,如果我们想使用成员函数该怎么办?
     class A
     {
     public:
         bool greater()
        {
            return true;
        }
     };
     我怎么对vector<A>中的元素执行这个greater?直接使用是不行了,只有采用的别的策略.

     在设计模式中有一种适配器模式,目的就是解决两种接口不兼容的问提,我们再回过头看看我们上面的问题,我们正是遇到这种不兼容的问题.摸板函数需要的类型和我们的准备提供的操作接口不兼容.(成员函数指针多了一个this,所以调用上和一般函数有所不同).

     当然你也可以多做一个版本的find_if来适应成员函数,下面的这个函数
     template<class _InIt,class _Type>
     inline _InIt mem_find_if(_InIt _First, _InIt _Last, bool (_Type::*_P)())
     { // find first satisfying _Pred
         for (; _First != _Last; ++_First)
                if (((*_First).*_P)())
                   break;
         return (_First);
     }

     现在就可以直接
     mem_find_if(coll.begin(),coll.end(),&A::greater);

     但这种方法有点象补漏洞的感觉,有没有更加优雅的,不用在在最基础的地方添加函数的方式呢?

     这里就是STL中的适配器的一个作用了,这里要谈到的适配器是mem_fun_ref,将成员函数接口转化成一个函数对象.我们先来看看他的使用:
     find_if(coll.begin(),coll.end(),mem_fun_ref(&A::greater));

     接下来让我们仔细看看这个适配器:
     template<class _Result,class _Ty> inline
    mem_fun_ref_t<_Result, _Ty> mem_fun_ref(_Result (_Ty::*_Pm)())
    { // return a mem_fun_ref_t functor adapter
        return (std::mem_fun_ref_t<_Result, _Ty>(_Pm));
    }

     这个函数返回一个mem_fun_ref_t<_Result, _Ty>对象(这个函数对象就是适配两个接口的对象),调用的是mem_fun_ref_t<_Result, _Ty>(_Pm))这个构造函数.在这里_Result是我们成员函数的返回值,_Ty是我们的类的类型,_Pm就是我们打算调用的成员函数的指针.


     我们再来看看我们真正传递给find_if的函数对象
     template<class _Result,class _Ty>
     class mem_fun_ref_t: public unary_function<_Ty, _Result>
     { // functor adapter (*left.*pfunc)(), non-const *pfunc
      public:
          explicit mem_fun_ref_t(_Result (_Ty::*_Pm)()): _Pmemfun(_Pm)
         { // construct from pointer
         }
        _Result operator()(_Ty& _Left) const
        { // call function
               return ((_Left.*_Pmemfun)());
         }
      private:
       _Result (_Ty::*_Pmemfun)(); // the member function pointer
     };


     这里最需要关注的就是operator(),这个函数有一个参数_Ty& _Left,这里的_Ty就是我们上面谈到的类的类型,这个operator()满足了find_if对于操作的要求.这个operator()的实质就是对于_Left调用Pmemfun,而这个Pmemfun在构造函数中初始化为我们传递的函数指针.

  这样就完成我们对于成员函数的调用了.这个适配器产生一个函数对象将原本不兼容的两个接口(find_if所需要的类型和成员函数类型)完美的兼容了.

引用地址:http://dev.csdn.net/article/21/21116.shtm

原创粉丝点击