仿函数进阶——组合型仿函数和自定能够使用函数配接器的仿函数

让自定仿函数也能够使用函数配接器:

对于自定仿函数。假设希望和函数配接器搭配使用,就必须满足某些条件:必须提供一些型别成员来反映其參数和返回值类型。为方便程序猿。标准库提供下面结构:

一元:

        template<class _Arg,class _Result>
	struct unary_function
	{	// base class for unary functions
	typedef _Arg argument_type;
	typedef _Result result_type;
	};
二元:

        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;
	};
如此一来,自定仿函数仅仅要继承两种形式之中的一个,就能轻松满足可配接的条件。*和使用ptr_fun效果一样,http://blog.csdn.net/ggz631047367/article/details/38058245

例:

template<class T1,class T2>
struct fopow:public binary_function<T1,T2,T1>
{
	T1 operator()(T1 base, T2 exp)const
	{
		return pow(base, exp);
	}
};

int main()
{
	vector<int> coll;
	for (int i = 1; i <= 9; ++i)
	{
		coll.push_back(i);
	}
	transform(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "), bind1st(fopow<float, int>(), 3));
	cout << endl;
	transform(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "), bind2nd(fopow<float, int>(), 3));
	cout << endl;
	system("pause");
	return 0;
}

输出:

3 9 27 81 243 729 2187 6561 19683
1 8 27 64 125 216 343 512 729

组合型仿函数:

 功能

 自己定义名称

SGI STL採用名称

 f(g(elem))

 compose_f_gx

compose1

 f(g(elem1,elem2))

 compose_f_gxy

 

 f(g(elem),h(elem))

 compose_f_gx_hx

compose2

 f(g(elem1),h(elem2))

 compose_f_gx_hy

 

一元组合函数配接器:

compose_f_gx的实作:先加10再乘4

template<class OP1, class OP2>
class compose_f_gx_t :public std::unary_function < typename OP2::argument_type, typename OP1::result_type > {
private:
	OP1 op1;//op1(op2(x))
	OP2 op2;
public:
	compose_f_gx_t(const OP1&o1, const OP2&o2) :op1(o1), op2(o2){}
	typename OP1::result_type operator()(const typename OP2::argument_type&x)const{
		return op1(op2(x));
	}
};

template<class OP1, class OP2>
inline compose_f_gx_t<OP1, OP2>  compose_f_gx(const OP1&o1, const OP2&o2)
{
	return compose_f_gx_t<OP1, OP2>(o1, o2);
}


int main()
{
	vector<int> coll;
	for (int i = 1; i <= 9; ++i)
	{
		coll.push_back(i);
	}
	transform(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "), 
		compose_f_gx(bind2nd(multiplies<int>(),4),bind2nd(plus<int>(),10)));
	cout << endl;
	
	system("pause");
	return 0;
}

compose_f_gx_hx的实作:大于4小于7

template<class OP1, class OP2, class OP3>
class compose_f_gx_hx_t :public std::unary_function<typename OP2::argument_type, typename OP1::result_type> {
private:
	OP1 op1;//op1(op2(x),op3(x))
	OP2 op2;
	OP3 op3;
public:
	compose_f_gx_hx_t(const OP1&o1, const OP2&o2, const OP3&o3) :op1(o1), op2(o2), op3(o3){}
	typename OP1::result_type operator()(const typename OP2::argument_type& x)const{
		return op1(op2(x), op3(x));
	}
};

template<class OP1, class OP2, class OP3>
inline compose_f_gx_hx_t<OP1, OP2, OP3>  compose_f_gx_hx(const OP1&o1, const OP2&o2, const OP3&o3)
{ 
	return compose_f_gx_hx_t<OP1, OP2, OP3>(o1, o2, o3);
}


int main()
{
	vector<int> coll;
	for (int i = 1; i <= 9; ++i)
	{
		coll.push_back(i);
	}
	vector<int>::iterator pos;
	pos=remove_if(coll.begin(), coll.end(), 
		compose_f_gx_hx(logical_and<bool>(),bind2nd(greater<int>(),4),bind2nd(less<int>(),7)));
	coll.erase(pos, coll.end());
	copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
	cout << endl;
	
	system("pause");
	return 0;
}

输出:

1 2 3 4 7 8 9

二元组合函数配接器:

compose_f_gx_hy的实:在一个字符串中以“区分大写和小写”的方式搜寻一个字符串

template<class OP1, class OP2, class OP3>
class compose_f_gx_hy_t :public std::binary_function<typename OP2::argument_type, 
	typename OP3::argument_type, typename OP1::result_type> {
private:
	OP1 op1;//op1(op2(x),op3(y))
	OP2 op2;
	OP3 op3;
public:
	compose_f_gx_hy_t(const OP1&o1, const OP2&o2, const OP3&o3) :op1(o1), op2(o2), op3(o3){}
	typename OP1::result_type operator()(const typename OP2::argument_type& x, 
		const typename OP3::argument_type&y)const{
		return op1(op2(x), op3(y));
	}
};

template<class OP1, class OP2, class OP3>
inline compose_f_gx_hy_t<OP1, OP2, OP3>  compose_f_gx_hy(const OP1&o1, const OP2&o2, const OP3&o3)
{ 
	return compose_f_gx_hy_t<OP1, OP2, OP3>(o1, o2, o3);
}


int main()
{
	string s("Internationalization");
	string sub("Nation");
	string::iterator pos;
	pos = search(s.begin(), s.end(), sub.end(), sub.end(), 
		compose_f_gx_hy(equal_to<int>(), ptr_fun(toupper), ptr_fun(toupper)));
	if (pos != s.end())
	{
		cout << """ << sub << "" is part of "" << s << """ << endl;
	}
	
	system("pause");
	return 0;
}

输出:

"Nation" is part of "Internationalization"




原文地址:https://www.cnblogs.com/zfyouxi/p/5154956.html