STL之函数对象(function object)

函数对象是定义了调用运算符的类,它们在STL中作为泛型算法的参数,用于替代函数指针。STL的函数对象具有可适配性,允许与函数适配器结合以实现更多功能。函数对象分为算术、关系和逻辑三类,如plus、equal_to和logical_and等。通过适配器,函数对象可以灵活地改变其行为,增强了算法的灵活性。
摘要由CSDN通过智能技术生成


1、概论

如果类定义了调用运算符(即圆括号),则该类的对象称作函数对象(function object)。因为可以调用这种对象,所以我们说这些对象的"行为像函数一样"。

函数对象常常作为泛型算法的实参,并代替函数指针使用。以sort()为例,sort(begin,end)接受两个迭代器作为其参数,默认利用元素类型的<运算符来实现排序。sort(begin,end,comp)则以用户定义的comp函数作为元素比较的根据。通常情况下,可以设计自己的比较函数comp,然后以函数指针当做算法的一个参数。但是函数指针不能满足STL对抽象性的要求,同时函数指针无法和STL其他组件搭配,产生更灵活的变化,因此需要使用函数对象。

STL的函数对象,根据操作数的个数划分,可分为一元和二元函数对象。根据功能划分,可分为算术运算、关系运算、逻辑运算三大类,每个类分别定义了一个执行命名操作的调用运算符。

在这里插入图片描述

2、可适配(adaptable)的关键

STL的函数对象可以使算法更灵活,而更加灵活的关键,在于STL的函数对象的可适配性(adaptability)。STL的函数对象应该有能力被函数适配器修饰,彼此像积木一样地串接。为了拥有适配能力,每一个函数对象必须定义自己的相应类型,就像迭代器如果要融入STL,也必须依照规定定义自己的5个相应类型一样,这些相应类型是为了让适配器能够取出,获得函数对象的某些信息。

函数对象的相应类型主要用来表现函数参数类型和返回值类型。标准库在<stl_function.h>中定义了两个类,分别代表一元函数对象和二元函数对象,其中没有任何数据成员或成员函数,只有一些类型定义。

template <typename _Arg, typename _Result>
struct unary_function
{
   
  /// @c argument_type is the type of the argument
  typedef _Arg argument_type;

  /// @c result_type is the return type
  typedef _Result result_type;
};

template <typename _Arg1, typename _Arg2, typename _Result>
struct binary_function
{
   
  /// @c first_argument_type is the type of the first argument
  typedef _Arg1 first_argument_type;

  /// @c second_argument_type is the type of the second argument
  typedef _Arg2 second_argument_type;

  /// @c result_type is the return type
  typedef _Result result_type;
};

3、算术类函数对象

算术类函数对象包括:plus<Type>、minus<Type>、multiplies<Type>、divides<Type>、modulus<Type>、negate<Type>。

template <typename _Tp>
struct plus : public binary_function<_Tp, _Tp, _Tp>
{
   
  _GLIBCXX14_CONSTEXPR
  _Tp operator()(const _Tp &__x, const _Tp &__y) const
  {
   
    return __x + __y;
  }
};

template <typename _Tp>
struct minus : public binary_function<_Tp, _Tp, _Tp>
{
   
  _GLIBCXX14_CONSTEXPR
  _Tp operator()(const _Tp &__x, const _Tp &__y) const
  {
   
    return __x - __y;
  }
};

template <typename _Tp>
struct multiplies : public binary_function<_Tp, _Tp, _Tp>
{
   
  _GLIBCXX14_CONSTEXPR
  _Tp operator()(const _Tp &__x, const _Tp &__y) const
  {
   
    return __x * __y;
  }
};

template <typename _Tp>
struct divides : public binary_function<_Tp, _Tp, _Tp>
{
   
  _GLIBCXX14_CONSTEXPR
  _Tp operator()(const _Tp &__x, const _Tp &__y) const
  {
   
    return __x / __y;
  }
};

template <typename _Tp>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值