版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/WizardtoH/article/details/83614970
上一篇:《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; }
};