C++标准库---组合型仿函数

一元组合函数配接器

最简单的组合型函数配接器,是将某个一元运算结果作为另外一个一元运算的输入。其实这只不过是嵌套调用两个一元仿函数。

例如,如果你要构造一个运算“先加10再乘以4”,就会用到这个函数配接器。


代码示例:

//compose_f_gx_t
#include<iostream>
#include<functional>
#include<vector>
#include<algorithm>
#include<iterator>

using namespace std;

template<class OP1,class OP2>
class compose_f_gx_t:public unary_function<typename OP2::argument_type,
	typename OP1::result_type>
{
private:
	OP1 op1;
	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);
}

void print(int elem)
{
	cout<<elem<<" ";
}

int main()
{
	vector<int> coll;

	for(int i=1;i<=9;i++)
	{
		coll.push_back(i);
	}

	for_each(coll.begin(),coll.end(),print);
	cout<<endl;

	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;
}

运行结果:

1 2 3 4 5 6 7 8 9

44 48 52 56 60 64 68 72 76


如何将两个准则加以逻辑组合,形成单一准则,例如,“大于4且小于7”。SGI STL 实做版本称之为compose2.


代码示例:

//compose_f_gx_hx_t
#include<functional>
#include<vector>
#include<algorithm>
#include<iostream>

using namespace std;

template<class OP1,class OP2,class OP3>
class compose_f_gx_hx_t:public unary_function<typename OP2::argument_type,typename OP1::result_type>
{
private:
	OP1 op1;
	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);
}

void print(int elem)
{
	cout<<elem<<" ";
}

int main()
{
	vector<int> coll;

	for(int i=1;i<=9;i++)
	{
		coll.push_back(i);
	}

	for_each(coll.begin(),coll.end(),print);
	cout<<endl;

	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());

	for_each(coll.begin(),coll.end(),print);
	cout<<endl;

	system("pause");
	return 0;
}

运行结果:

1 2 3 4 5 6 7 8 9

1 2 3 4 7 8 9


二元组合函数配接器

二元组合函数配接器,可以将两个一元运算(分别接受不同参数)的结果加以处理。


代码示例:

//二元组合函数配接器
#include<iostream>
#include<functional>
#include<algorithm>
#include<vector>
#include<string>
#include<cctype>

using namespace std;

template<class OP1,class OP2,class OP3>
class compose_f_gx_hy_t:public binary_function<typename OP2::argument_type,
	typename OP3::argument_type,typename OP1::result_type>
{
private:
	OP1 op1;
	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="lanzhihui";
	string sub="ZhI";

	string::iterator pos;

	pos=search(s.begin(),s.end(),sub.begin(),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;
	}
	else
	{
		cout<<"Not Found"<<endl;
	}

	system("pause");
	return 0;
}

运行结果:

"ZhI" is part of "lanzhihui"


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值