C++中的标准函数对象

转载地址:http://blog.csdn.net/chollima/article/details/3525934

函数对象
函数对象是一种利用句法规则特定设计的类生成的对象,它的作用类似于函数。
在C++中,这是通过在类中定义成员函数operator()实现的,举个例子:
struct myclass {
    int operator()(int a) {return a;}
}myobject;
int x = myobject(0);
它们作为断定或者比较功能和标准算法(algorithms)一起使用时将非常有用。
在头文件<functional>中标准库提供了好几种标准的函数对象和一些修改方法以适应它们自己的行为。
基类
unary_function  一元的函数对象基类
template <class Arg, class Result>
  struct unary_function {
    typedef Arg argument_type;
    typedef Result result_type;
  };
binary_function 二元的函数对象基类
template <class Arg1, class Arg2, class Result>
  struct binary_function {
    typedef Arg1 first_argument_type;
    typedef Arg2 second_argument_type;
    typedef Result result_type;
  };
算子类
算术操作符
plus        +   加函数对象类  
template <class T> struct plus : binary_function <T,T,T> {
  T operator() (const T& x, const T& y) const
    {return x+y;}
};
minus       -   减函数对象类
同理,这里就不一一列举
multiplies  *   乘函数对象类
divides     /   除函数对象类
modulus     %   模函数对象类
negate      -   负函数对象类
比较操作符
equal_to    ==  函数对象类用于相等比较
template <class T> struct equal_to : binary_function <T,T,bool> {
  T operator() (const T& x, const T& y) const
    {return x==y;}
};
not_equal_to    !=  函数对象类用于不相等比较
同理,这里就不一一列举
greater     >   函数对象类用于大于比较
less        <   函数对象类用于小于比较
greater_equal   >=  函数对象类用于不小于比较
less_equal  <=  函数对象类用于不大于比较
逻辑操作符
logical_and &&  逻辑与函数对象类
logical_or  ||  逻辑或函数对象类
logical_not !   逻辑非函数对象类
适配器和转换功能
否定器
not1    返回一元函数对象的反面
template <class Predicate>
unary_negate<Predicate> not1 (const Predicate& pred)
{
  return unary_negate<Predicate>(pred);
}
not2    返回二元函数对象的方面
template <class Predicate>
binary_negate<Predicate> not2 (const Predicate& pred)
{
  return binary_negate<Predicate>(pred);
}
参数绑定器
bind1st     返回绑定了第一个参数的函数对象
template <class Operation, class T>
binder1st<Operation> bind1st (const Operation& op, const T& x)
{
  return binder1st<Operation>(op, typename Operation::first_argument_type(x));
}
bind2nd     返回绑定了第二个参数的函数对象
template <class Operation, class T>
binder2nd<Operation> bind2nd (const Operation& op, const T& x)
{
  return binder2nd<Operation>(op, typename Operation::second_argument_type(x));
}
有用的类型
unary_negate    生成一元函数对象类的反面
template <class Predicate> class unary_negate
  : public unary_function <typename Predicate::argument_type,bool>
{
protected:
  Predicate fn;
public:
  explicit unary_negate ( const Predicate& pred ) : fn (pred) {}
  bool operator() (const typename Predicate::argument_type& x) const
  { return !fn(x); }
};
binary_negate   生成二元函数对象类的反面
template <class Predicate> class binary_negate
  : public binary_function <typename Predicate::first_argument_type,
                            typename Predicate::second_argument_type, bool>
{
protected:
  Predicate fn;
public:
  explicit binary_negate ( const Predicate& pred ) : fn (pred) {}
  bool operator() (const typename Predicate::first_argument_type& x,
                   const typename Predicate::second_argument_type& y) const
  { return !fn(x,y); }
};
binder1st   绑定了第一个参数的函数对象类
template <class Operation> class binder1st
  : public unary_function <typename Operation::second_argument_type,
                           typename Operation::result_type>
{
protected:
  Operation op;
  typename Operation::first_argument_type value;
public:
  binder1st ( const Operation& x,
              const typename Operation::first_argument_type& y) : op (x), value(y) {}
  typename Operation::result_type
    operator() (const typename Operation::second_argument_type& x) const
    { return op(value,x); }
};
binder2nd   绑定了第二个参数的函数对象类
template <class Operation> class binder2nd
  : public unary_function <typename Operation::first_argument_type,
                           typename Operation::result_type>
{
protected:
  Operation op;
  typename Operation::second_argument_type value;
public:
  binder2nd ( const Operation& x,
              const typename Operation::first_argument_type& y) : op (x), value(y) {}
  typename Operation::result_type
    operator() (const typename Operation::first_argument_type& x) const
    { return op(x,value); }
};
还有一小部分没有翻译(这部分我还没看呢),以后另起补上。



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值