C++ STL和泛型编程(三)----仿函数Functors

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;
但是其继承的是类型别名的声明定义,所以实际上继承前后的大小不变!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值