SGISTL源码-仿函数 笔记

functor

仿函数的行为类似于函数,但其可以作为算法的比较策略(优化算法的使用)

仿函数是一种重载了operator()的模板类,主要用于STL中作为算法的判断条件。

分类

操作数的个数上分,可以将仿函数划分为一元仿函数和二元仿函数。

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)、减法(minus)、乘法(multiplies)、除法(divides)
  • 逻辑运算类 — 与(logical_and)、或(logical_or)、非(logical_not)
  • 关系运算类 — 等于(equal_to)、大于等于(greater_equal)、小于(less)

下面具体介绍相关仿函数。

算数类仿函数
// 加
template <class _Tp>
struct plus : public binary_function<_Tp,_Tp,_Tp> {
    _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x + __y; }
};

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

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

// 除
template <class _Tp>
struct divides : public binary_function<_Tp,_Tp,_Tp> {
    _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x / __y; }
};
//取余
template <class _Tp>
struct modulus : public binary_function<_Tp,_Tp,_Tp> 
{
    _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x % __y; }
};
// 取反
template <class _Tp>
struct negate : public unary_function<_Tp,_Tp> 
{
    _Tp operator()(const _Tp& __x) const { return -__x; }
};
关系运算类仿函数
// 等于
template <class _Tp>
struct equal_to : public binary_function<_Tp,_Tp,bool> 
{
    bool operator()(const _Tp& __x, const _Tp& __y) const { return __x == __y; }
};

// 不等于
template <class _Tp>
struct not_equal_to : public binary_function<_Tp,_Tp,bool> 
{
    bool operator()(const _Tp& __x, const _Tp& __y) const { return __x != __y; }
};

// 大于
template <class _Tp>
struct greater : public binary_function<_Tp,_Tp,bool> 
{
    bool operator()(const _Tp& __x, const _Tp& __y) const { return __x > __y; }
};
// 小于
template <class _Tp>
struct less : public binary_function<_Tp,_Tp,bool> 
{
    bool operator()(const _Tp& __x, const _Tp& __y) const { return __x < __y; }
};

// 大于等于
template <class _Tp>
struct greater_equal : public binary_function<_Tp,_Tp,bool>
{
    bool operator()(const _Tp& __x, const _Tp& __y) const { return __x >= __y; }
};

// 小于等于
template <class _Tp>
struct less_equal : public binary_function<_Tp,_Tp,bool> 
{
    bool operator()(const _Tp& __x, const _Tp& __y) const { return __x <= __y; }
};
逻辑运算类仿函数
// 逻辑与
template <class _Tp>
struct logical_and : public binary_function<_Tp,_Tp,bool>
{
    bool operator()(const _Tp& __x, const _Tp& __y) const { return __x && __y; }
};

// 逻辑或
template <class _Tp>
struct logical_or : public binary_function<_Tp,_Tp,bool>
{
    bool operator()(const _Tp& __x, const _Tp& __y) const { return __x || __y; }
};

// 逻辑非
template <class _Tp>
struct logical_not : public unary_function<_Tp,bool>
{
    bool operator()(const _Tp& __x) const { return !__x; }
};
Functor Adapter
not1

创建返回传递的一元谓词的反义的函数对象

template <class _Predicate>
class unary_negate : public unary_function<typename _Predicate::argument_type, bool> {
protected:
    _Predicate _M_pred;
public:
    explicit unary_negate(const _Predicate& __x) : _M_pred(__x) {}
    bool operator()(const typename _Predicate::argument_type& __x) const {
        return !_M_pred(__x);
        //operator()重载
  }
};

template <class _Predicate>
inline unary_negate<_Predicate>  not1(const _Predicate& __pred)
{
    return unary_negate<_Predicate>(__pred);
}
not2

同理,创建返回传递的二元谓词的反义的函数对象。

template <class _Predicate> 
class binary_negate 
  : public binary_function<typename _Predicate::first_argument_type,
                           typename _Predicate::second_argument_type,
                           bool> {
protected:
    _Predicate _M_pred;
public:
    explicit binary_negate(const _Predicate& __x) : _M_pred(__x) {}
    bool operator()(const typename _Predicate::first_argument_type& __x, 
                    const typename _Predicate::second_argument_type& __y) const {
        return !_M_pred(__x, __y); 
  }
};

template <class _Predicate>
inline binary_negate<_Predicate>  not2(con
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值