STL中的函数对象(一)

来源:互联网 发布:服务器数据备份方案 编辑:程序博客网 时间:2024/06/05 14:37

为使类属性算法具有灵活性,STL常用函数重载机制为算法提供两种形式,算法的第一种形式使用的是常规操作来实现目标。在第二种形式中,算法可以根据用户指定的准则对元素进行处理。这种准则是通过函数对象来传递的。函数对象世纪上是重载了operator()的类模版。

STL提供了许多函数对象,这些对象包含在头文件<functional>中。


函数对象说明1、算术函数对象:plus<T>x+yminus<T>x-ymultiplies<T>x*ydivides<T>x/ymodulus<T>x%ynegate<T>-x2、关系(比较)函数对象:equal_to<T>x==ynot_equal_to<T>x!=ygreater<T>x>ygreater_equal<T>x>=yless<T>x<yless_equal<T>x<=y3、逻辑函数对象: logical_not<T>!xlogical_and<T>x&&ylogical_or<T>x||y

 STL中包含许多预定义的函数对象,其中有:

  1. 算术操作:plus, minus, multiplies, divides, modulus, 和 negate,例:

    assert(V2.size() >= V1.size() && V3.size() >= V1.size());
    transform(V1.begin(), V1.end(), V2.begin(), V3.begin(),modulus<int>());

    把V1[i]%V2[i]存入V3[i]。

     

  2. 比较操作:equal_to, not_equal_to, greater, less, greater_equal, 和 less_equal,例:

    list<int>::iterator first_nonnegative = find_if(L.begin(), L.end(), bind2nd(greater_equal<int>(), 0));

    在L中找到第一个大于等于0的元素。

     

  3. 逻辑操作:logical_and, logical_or, 和 logical_not,例:

    char str[MAXLEN];
    const char* wptr = find_if(str, str + MAXLEN,compose2(logical_or<bool>(),bind2nd(equal_to<char>(), ' '),bind2nd(equal_to<char>(), '\n')));

    在str中找到第一个空格或行尾。

    以上三类都是模板类,要生成一个函数对象需要实例化。同时在使用时,需要注意:

    • 算法需要的参数是什么类型的函数对象,是一元函数还是二元函数。
    • 输入容器内元素类型是否与函数对象参数兼容,输出容器内元素是否与函数对象返回值兼容。

     

  4. 修改、组装已有函数对象生成新的函数对象:unary_compose, binary_compose, unary_negate, binary_negate, binder1st, binder2nd, pointer_to_unary_function, pointer_to_binary_function, mem_fun_t, mem_fun_ref_t, mem_fun1_t 和mem_fun1_ref_t。这类函数对象就是所谓的函数对象适配器,由于书写生涩,一般不会直接实例化,都有对应的辅助函数(一般都是内联型)简化调用。
函数对象模板类名辅助函数名说明unary_composecompose1unary_compose(const AdaptableUnaryFunction1& f,
              const AdaptableUnaryFunction2& g);

生成一个一元函数对象,操作为“f(g(x))”;vc6/7自带stl没有定义。

binary_composecompose2binary_compose(const AdaptableBinaryFunction& f, 
               const AdaptableUnaryFunction1& g1, 
               const AdaptableUnaryFunction1& g2);

生成一个二元函数对象,操作为“f(g1(x), g2(x))”;vc6/7自带stl没有定义。

unary_negatenot1unary_negate(const AdaptablePredicate& pred);

一元谓词取非。

binary_negatenot2binary_negate(const AdaptableBinaryPredicate& pred);

二元谓词取非。

binder1stbind1stbinder1st(const AdaptableBinaryFunction& F,
          AdaptableBinaryFunction::first_argument_type c);

把二元函数对象的第一个参数绑定为c,生成一个一元函数对象。

binder2nd bind2ndbinder2nd(const AdaptableBinaryFunction& F,
          AdaptableBinaryFunction::second_argument_type c);

把二元函数对象的第二个参数绑定为c,生成一个一元函数对象。

pointer_to_unary_functionptr_fun把普通全局函数或全局函数指针(只能有1个或2个参数)转换成函数对象。

ptr_fun有两个原型分别生成pointer_to_unary_function对象和pointer_to_binary_function对象。

pointer_to_binary_functionmem_fun_tmem_fun把某个类的成员函数(只能有0个或1个参数,因为类成员函数的第一个参数已定义为this指针)转换成函数对象。

mem_fun有两个原型分别生成mem_fun_t对象(是个一元函数对象)和mem_fun1_t对象(是个二元函数对象)。

mem_fun1_tmem_fun_ref_tmem_fun_ref类似mem_fun,区别如下:

struct T{
     print(){}
};

list<T*> lstT;
for_each(lstT.begin(),lstT.end(),mem_fun(&T::print));

list<T> lstT;
for_each(lstT.begin(),lstT.end(),mem_fun_ref(&T::print));

mem_fun1_ref_t   
0 0