系列文章目录
STL源码剖析笔记——迭代器
STL源码剖析笔记——vector
STL源码剖析笔记——list
STL源码剖析笔记——deque、stack,queue
STL源码剖析笔记——Binary Heap、priority_queue
STL源码剖析笔记——AVL-tree、RB-tree、set、map、mutiset、mutimap
STL源码剖析笔记——哈希表、unordered_set、unordered_map、unordered_mutiset、unordered_mutimap
STL源码剖析笔记——仿函数(函数对象)
STL源码剖析笔记——适配器(adapters)
文章目录
1. 定义
仿函数(functors)在C++标准规格定案后所采用的新名称是函数对象(function objects),是一种具有函数特质的对象。仿函数通过重载operator( )实现函数行为,实际上就是一个“行为类似函数”的对象。
2. 型别
仿函数的相应型别主要用来表现函数参数型别和传回值型别。为了方便起见, <stl_function.h>定义了两个classes,分别代表一元仿函数和二元仿函数(STL不支持三元仿函数),其中没有任何data members或member functions,唯有一些型别定义。任何仿函数,只要依个人需求选择继承其中一个class,便自动拥有 了那些相应型别,也就自动拥有了配接能力。
(1) 一元函数(unary_function)
unary_function的定义为:
/ / STL规定,每一个Adaptable Unary Function都应该继承此类别
template <class Arg, class Result>
struct unary_function {
typedef Arg argument_type;
typedef Result result_type;
};
一元函数拥有一个参数型别和一个返回值型别,可以通过继承这个类来获取型别。
//以下仿函数继承了 unary__function.
template <class T>
struct negate:public unary_function<T, T> {
T operator()(const T& x) const {
return -x;}
};
(2) 二元函数(binary_function)
binary_function的定义为:
/ / STL规定,每一个Adaptable Binary Function都应该继承此类别
template <class Argl, class Arg2 , class Result>
struct binary_function {
typedef Argl first_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;
};
二元函数拥有两个参数型别和一个返回值型别,可以通过继承这个类来获取型别。
/ /以下仿函数继承了 binary-f unction.
template <class T>
struct plus:public binar_functior<T, T, T> {
T operator()(const T& x, const T& y) const {
return x + y; }
};
3. 算术类 (Arithmetic)仿函数
STL内建的“算术类仿函数”,支持加法、减法、乘法、除法、模数(余数, modulus)和否定(negation)运算。除了 “否定”运算为一元运算,其它都是二元运算。
•加法:plus< T >
•减法: minus< T >
•乘法:multiplies< T >
•除法:divides< T >
•模取(modulus) :modulus< T >
• 否定(negation) :negate< T >
//以下6个为算术类(Arithmetic)仿函数
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& xz 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_functio<T, T> {
T operator()(const T& x) const {
return -x; }
};
一个仿函数有两种用法:第一种是实例化对象,并且运用函数功能
plus<int> plusobj;//创建对象
cout << plusobj(3,5) << endl;调用()重载,输出为8
第二种是创建一个临时对象,并且运用函数功能
//以下直接以仿函数的临时对象履行函数功能
//语法分析:functor<T> ()是一个临时对象,后面再接一对小括号
//意指调用operator()
cout << plus<int>()(3,5) << endl; // 8
4. 关系运算类 (Relational ) 仿函数
STL内建的“关系运算类仿函数”支持了等于、不等于、大于、大于等于、 小于、小于等于六种运算。每一个都是二元运算。
•等于(equality) :equal_to< T >
•不等于(inequality) : not_equal_to< T >
•大于(greater than) : greater< T >
•大于或等于(greater than or equal) : greater_equal< T >
•小于(lessthan) : less< T >
•小于或等于(less than or equal) : less_equal< T >
//以下6个为关系运算类(Relational)仿函数
template <class T>
struct equal_to:public binar_function<T, T, bool> {
bool operator()(const T& x, const T& y) const {
return x == y; }
};
template <class T>
struct not_equal_to:public binar_function<T, T, bool> {
bool operator()(const T& xf const T& y) const {
return x 1= y;}
};
template <class T>
struct greater:public binar_function<T, T, bool> {
bool operator()(const T& x, const T& y) const {
return x > y;}
};
template <class T>
struct less:public binar_function<T, T, bool> {
bool operator()(const T& x, const T& y) const {
return x < y;}
};
template <class T>
struct greater_equal:public binar_function<T, T, bool> {
bool operatort)(const T& xz const T& y) const {
return x >= y;}
};
template <class T>
struct less_equal:public binar_function<T, T, bool> {
bool operator()(const T& x, const T& y) const {
return x <= y;}
};
5. 逻辑运算类(Logical)仿函数
STL内建的“逻辑运算类仿函数”支持了逻辑运算中的And、Or、Not三 种运算,其中And和Or为二元运算,Not为一元运算。
•逻辑运算 And: logical_and< T >
•逻辑运算 Or: logical_or< T >
•逻辑运算 Not: logical_not< T >
//以下3个为逻辑运算类(Logical)仿函数
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 binary_function<T, T, bool> {
bool operator()(const T& x) const {
return !x; }
};