函数对象 也叫仿函数
行为类似函数的对象
就是重载 "()" 操作符,使得类对象可以想函数那样调用
注:函数对象(仿函数)是一个类,不是一个函数
假定某个类有一个重载的operator(),而且重载的operator()要求获取一个参数,我们就将这个类称为"一元仿函数"(unary functor);
如果重载的operator()要求获取两个参数,我们就将这个类称为“二元仿函数”(binary functor).
谓词
是指普通函数或重载的operatop()返回值是bool类型的函数对象(仿函数)。
如果operator接受一个参数,就叫一元谓词
如果operator接受二个参数,就叫二元谓词
谓词可作为一个判断式;
一元函数对象,应用举例:for_each();
一元谓词 应用举例:find_if();
二元函数对象 应用举例:transform();
二元谓词 应用举例:sort();
内建函数对象
stl内建了一些函数对象
需要引入头文件#include<functional>
算数类函数对象: 注:除了negate是一元运算,其他都是二元运算
template<class T> T plus<T> 加法仿函数
template<class T> T minute<T> 减法仿函数
template<class T> T multiplies<T> 乘法仿函数
template<class T> T divides<T> 除法仿函数
template<class T> T modulus<T> 取模仿函数
template<class T> T negate<T> 取反仿函数
关系类函数对象: 都是二元运算
template<class T> bool equal_to<T> 等于
template<class T> bool not_equal_to<T> 不等于
template<class T> bool greater<T> 大于
template<class T> bool greater_equal<T> 大于等于
template<class T> bool less<T> 小于
template<class T> bool less_equal<T> 小于等于
逻辑类函数对象: not是一元,其他是二元
template<class T> bool logical_and<T> 逻辑与
template<class T> bool logical_or<T> 逻辑或
template<class T> bool logical_not<T> 逻辑非
函数对象适配器:
函数对象适配器是完成一些配接工作,这些配接包括绑定(bind),否定(negate),
以及对一般函数或成员函数的修饰,使其成为函数对象,
一元函数对象要继承于: unary_function<参数1,参数3>
二元函数对象要继承于: binary_function<参数1,参数2,参数3>
参数1,2 是函数对象要传的参数类型,参数3是返回值的类型
重载的operator()函数还要加上const
重点:
1、bind1st 将参数绑定为函数对象的第一个参数
2、bind2en 将参数绑定为函数对象的第二个参数
上面两个将二元对象转变为一元对象
3、not1 对一元函数对象取反
4、not2 对二元函数对象取反
prt_fun 将普通函数修饰成成员函数
mem_fun 修饰成员函数
mem_fun_ref 修饰成员函数
行为类似函数的对象
就是重载 "()" 操作符,使得类对象可以想函数那样调用
注:函数对象(仿函数)是一个类,不是一个函数
假定某个类有一个重载的operator(),而且重载的operator()要求获取一个参数,我们就将这个类称为"一元仿函数"(unary functor);
如果重载的operator()要求获取两个参数,我们就将这个类称为“二元仿函数”(binary functor).
谓词
是指普通函数或重载的operatop()返回值是bool类型的函数对象(仿函数)。
如果operator接受一个参数,就叫一元谓词
如果operator接受二个参数,就叫二元谓词
谓词可作为一个判断式;
一元函数对象,应用举例:for_each();
一元谓词 应用举例:find_if();
二元函数对象 应用举例:transform();
二元谓词 应用举例:sort();
内建函数对象
stl内建了一些函数对象
需要引入头文件#include<functional>
算数类函数对象: 注:除了negate是一元运算,其他都是二元运算
template<class T> T plus<T> 加法仿函数
template<class T> T minute<T> 减法仿函数
template<class T> T multiplies<T> 乘法仿函数
template<class T> T divides<T> 除法仿函数
template<class T> T modulus<T> 取模仿函数
template<class T> T negate<T> 取反仿函数
关系类函数对象: 都是二元运算
template<class T> bool equal_to<T> 等于
template<class T> bool not_equal_to<T> 不等于
template<class T> bool greater<T> 大于
template<class T> bool greater_equal<T> 大于等于
template<class T> bool less<T> 小于
template<class T> bool less_equal<T> 小于等于
逻辑类函数对象: not是一元,其他是二元
template<class T> bool logical_and<T> 逻辑与
template<class T> bool logical_or<T> 逻辑或
template<class T> bool logical_not<T> 逻辑非
函数对象适配器:
函数对象适配器是完成一些配接工作,这些配接包括绑定(bind),否定(negate),
以及对一般函数或成员函数的修饰,使其成为函数对象,
一元函数对象要继承于: unary_function<参数1,参数3>
二元函数对象要继承于: binary_function<参数1,参数2,参数3>
参数1,2 是函数对象要传的参数类型,参数3是返回值的类型
重载的operator()函数还要加上const
重点:
1、bind1st 将参数绑定为函数对象的第一个参数
2、bind2en 将参数绑定为函数对象的第二个参数
上面两个将二元对象转变为一元对象
3、not1 对一元函数对象取反
4、not2 对二元函数对象取反
prt_fun 将普通函数修饰成成员函数
mem_fun 修饰成员函数
mem_fun_ref 修饰成员函数