C++的STL适配器的实现原理

在C++的算法中,每个仿函数,都必须要继承binary_function 或者unary_function这两个类, 这两个函数都是定义typedef定义;

这里可以看源码;

template<class _Arg1,
	class _Arg2,
	class _Result>
	struct binary_function
	{	// base class for binary functions
	typedef _Arg1 first_argument_type;
	typedef _Arg2 second_argument_type;
	typedef _Result result_type;
	};
template<class _Arg,
	class _Result>
	struct unary_function
	{	// base class for unary functions
	typedef _Arg argument_type;
	typedef _Result result_type;
	};

他们的主要做用是为每一个不同的仿函数的设置项同的类型标志,以便后面的模板函数或者模板类使用时可以直接用类型标志调用,而不必用户自己关心;怎么说可以不能理解, 但是通过源码你就可以理解这种设计思路多好;

我先将实现的源代码贴出来;

# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;

//定义另一个类模板,即是仿函数, 可以做比大小之用;
template <typename T>
struct MyGreater : public binary_function<T, T, bool>//继承二元函数,指定类模板的类型;
{
	bool operator()(T a, T b) const
	{
		return (a > b);
	}
};


template <typename FunctorType>
class Binder2nd : public unary_function <typename FunctorType::first_argument_type, typename FunctorType::result_type>
{
public:
	//将模板类型参数中的类型重命名,这里就体现除了,继承binary_function 和unary_function的好出;
	typedef typename FunctorType::first_argument_type first_type;
	typedef typename FunctorType::second_argument_type second_type;
	typedef typename FunctorType::result_type result_type;
public:
	Binder2nd(const FunctorType & functor, const second_type & value)//构造函数, 用于构造一个匿名的对象用于返回;
		: FunctorObject(functor), BindValue(value) {}

	result_type operator()(const first_type & ValuePassByAlgorithm) const
	{
		//这里是实现函数绑定的关键, 此时的FunctorObject就是你绑定的仿函数的那个仿函数, 这里就实现了绑定;
		return FunctorObject(ValuePassByAlgorithm, BindValue);
	}
protected:
	FunctorType FunctorObject;//接受桥梁函数传递过来的函数对象了;
	second_type BindValue;//接收绑定值;
};

//这里设计有一个技巧;用了函数的类型自动推导, 具体的后面说;
template <typename FunctorType, typename BindType>
inline Binder2nd<FunctorType> Bind2nd(const FunctorType & FunctorObject, const BindType & BindValue)
{
	typedef typename FunctorType::second_argument_type second_type;//键仿函数的对象的第二个参数类型重新命名, 不然太长了;
	//放回一个匿名的对象, 类型是Binder2nd<FunctorType>,当前这个模板函数是桥梁,主要作用是推导函数对象的类型;
	return Binder2nd<FunctorType>(FunctorObject, second_type(BindValue));
}

//这里的模式和上面的Bind2nd基本是一致的, 
template <typename FunctorType>
class Negator : public unary_function<typename FunctorType::argument_type, bool>
{
public:
	explicit Negator(const FunctorType & functor)
		: FunctorObject(functor) {}
	bool operator()(const typename FunctorType::argument_type & ValuePassByAlgorithm) const
	{
		return !FunctorObject(ValuePassByAlgorithm);
	}
protected:
	FunctorType FunctorObject;
};

template <typename FunctorType>
inline Negator<FunctorType> Not1(const FunctorType & FunctorObject)
{
	return Negator<FunctorType>(FunctorObject);
}

void Run01(void)
{
	vector<int> v;
	for (int i = 0; i < 1000; i++)
	{
		v.push_back(i + 1);
	}

	auto num = count_if(v.begin(), v.end(), Not1(Bind2nd(MyGreater<int>(), 500)));
	cout << "num = " << num << endl;
}

int main(int argc, char ** argv)
{
	Run01();


	system("pause");
	return 0;
}

上面的实现基本思路是通过桥梁函数判断类型, 后定义一个类, 用类型嵌套的类型定义相关的成员变量, 后再累中重载()

然后在这里重载函数中是实现绑定或者否定; 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值