《STL源码剖析》笔记-仿函数(函数对象)

上一篇:《STL源码剖析》笔记-算法

仿函数也就是函数对象,是一种具有函数特质的对象,能够像函数一样被调用,但是实际上不是函数而是一个对象。在上一篇介绍算法的文章中,各种算法往往都有两个版本,其中一个版本允许指定一个仿函数或者函数指针;这是仿函数的一个用法,更重要的是仿函数能配合配接器(后续文章会进行介绍)进行灵活的使用。

下面来看一下仿函数的实现和使用的例子:

// STL预定义,作为二元仿函数的基类,输入两个参数,并指定返回值
template <class Arg1, class Arg2, class Result>
struct binary_function {
    typedef Arg1 first_argument_type;
    typedef Arg2 second_argument_type;
    typedef Result result_type;
};      

// STL提供的greater仿函数,用于判断输入的两个元素,前一个是否较大
template <class T>
struct greater : public binary_function<T, T, bool> {
    bool operator()(const T& x, const T& y) const { return x > y; }   // 重载了()操作符,实现了函数式的调用
};

int main()
{
    std::greater<int> igreater;
    std::cout << igreater(1, 2) << std::endl;     // 0
    std::cout <<  std::greater<int>()(2, 1) << std::endl;     // 1

    return 0;
}

STL仿函数型别定义

上面的例子中写到了binary_function结构体作为二元仿函数的基类,另外还有一元仿函数的结构体unary_function。其中定义了输入参数和返回值的型别,这些型别让仿函数有了配接的能力。

template <class Arg, class Result>
struct unary_function {
    typedef Arg argument_type;
    typedef Result result_type;
};

template <class Arg1, class Arg2, class Result>
struct binary_function {
    typedef Arg1 first_argument_type;
    typedef Arg2 second_argument_type;
    typedef Result result_type;
};    

算术类仿函数

// 加法
template <class T>
struct plus : public binary_function<T, T, T> {
    T operator()(const T& x, const T& y) const { return x + y; }
};

// 减法
template <class T>
struct minus : public binary_function<T, T, T> {
    T operator()(const T& x, const T& y) const { return x - y; }
};

// 乘法
template <class T>
struct multiplies : public binary_function<T, T, T> {
    T operator()(const T& x, const T& y) const { return x * y; }
};

// 除法
template <class T>
struct divides : public binary_function<T, T, T> {
    T operator()(const T& x, const T& y) const { return x / y; }
};

// 取余
template <class T>
struct modulus : public binary_function<T, T, T> {
    T operator()(const T& x, const T& y) const { return x % y; }
};

// 取负数
template <class T>
struct negate : public unary_function<T, T> {
    T operator()(const T& x) const { return -x; }
};

关系运算类仿函数

// 等于
template <class T>
struct equal_to : public binary_function<T, T, bool> {
    bool operator()(const T& x, const T& y) const { return x == y; }
};

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

// 大于
template <class T>
struct greater : public binary_function<T, T, bool> {
    bool operator()(const T& x, const T& y) const { return x > y; }
};

// 小于
template <class T>
struct less : public binary_function<T, T, bool> {
    bool operator()(const T& x, const T& y) const { return x < y; }
};

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

// 小于等于
template <class T>
struct less_equal : public binary_function<T, T, bool> {
    bool operator()(const T& x, const T& y) const { return x <= y; }
};

逻辑运算类仿函数

// 与
template <class T>
struct logical_and : public binary_function<T, T, bool> {
    bool operator()(const T& x, const T& y) const { return x && y; }
};

// 或
template <class T>
struct logical_or : public binary_function<T, T, bool> {
    bool operator()(const T& x, const T& y) const { return x || y; }
};

// 非
template <class T>
struct logical_not : public unary_function<T, bool> {
    bool operator()(const T& x) const { return !x; }
};

下一篇:《STL源码剖析》笔记-配接器adapters

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值