C++ STL和泛型编程(三)----仿函数和函数对象(Functors)
一、结构形式
Functors是一个struct即类,里面operator(),所以最终是创建出来一个对象,而因为这个对象起到像函数的作用因而称为仿函数。其只为算法提供服务。
这里的Functors都继承了binary_function<T, T, T>类。
由上图左下角的Algorithm的第三传入模板参数知,其需要一种函数或仿函数去告诉它如何操作:
这里的myfunc便是第三传入模板参数为函数的情况,而 myobj, less<int>(), greater<int>()【需要创建一个对象传进去】 都是第三传入模板参数为仿函数【实际为一个类对象,但此对象实现的是类似函数的功能,因而称为仿函数】 的情况。
另外上面的仿函数myclas没有继承binary_function<T, T, T>类或unuary_function<T, T>类,所以可能无法满足 Function Adapter(函数适配器) 的一些提问要求。
二、仿函数functors的可适配(adaptable)条件
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;
}
STL规定每个仿函数都必须继承binary_function<T, T, T>或unary_function<T, T>中的一个以满足Function Adapter的提问:
因为functors被adapter去改造(提问)时,需要提供如argument_type, result_type【继承unary_function<T, T>时】或first_argument_type, second_argument_type, result_type【继承binary_function<T, T, T>时】给adapter
- 继承unary_function的functors
template<class T1, class T2>
struct pair{
typedef T1 first_type;
typedef T2 second_type;
T1 first;
T2 second;
pair(): first(T1()), second(T2()){} // 初始化且通过T1()临时对象的方式赋初值
pair(const T1& a, const T2& b): first(a), second(b){} // 构造函数
};
template<class T>
struct identity: public unary_function<T, T>{
const T& operator()(const T& x) const{
return x;
}
};
template<class Pair>
struct select1st: public unary_function<Pair, typename Pair::first_type>{
const typename Pair::first_type& operator()(const Pair& x) const{ // typename表示后面的是一种类型
return x.first;
}
};
template<class Pair>
struct select2nd: public unary_function<Pair, typename Pair::first_type>{
const typename Pair::second_type& operator()(const Pair& x) const{
return x.second;
}
};
- 继承binary_function的functors
template<class T>
struct less: public binary_function<T, T, bool>{
bool operator()(const T& x, const T& y) const{
return x < y;
}
};
less<int>由于继承而有了3个typedef:
typedef int first_argument_type;
typedef int second_argument_type;
typedef bool result_type;
但是其继承的是类型别名的声明定义,所以实际上继承前后的大小不变!