函数配接器

组合函数配接器的可能表现形式
功能名称SGI STL的名称
f(g(elem))compose_f_gxcompose1
f(g(elem,elem2))compose_f_gxy 
f(g(elem),h(elem))compose_f_gx_hxcompose2
f(g(elem),h(elem2))compose_f_gx_hy 
看不懂请了解 点击打开链接


1.一元组合函数配接器

#include<iostream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
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 myop1;
	op2 myop2;
public:
	compose_f_gx_t(const op1&o1, const op2&o2) :myop1(o1), myop2(o2){};
	typename op1::result_type operator()( const typename op2::argument_type &x)
	{
		return myop1(myop2(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);
}


class sequence
{
private:
	int value;
public:
	sequence(int n) :value(n){};
	int operator ()()
	{
		return value++;
	}
};
void main()
{
	vector<int>vt;
	generate_n(back_inserter(vt), 10, sequence(1));
	copy(vt.begin(), vt.end(), ostream_iterator<int>(cout, ","));
	cout << endl;
	//实现了对容器中的每个元素先加10在乘5,并且输出到屏幕上
	transform(vt.begin(), vt.end(), ostream_iterator<int>(cout, "--"),
		compose_f_gx(bind2nd(multiplies<int>(), 5), bind2nd(plus<int>(), 10)));
	system("pause");
}

//组合“两个运作规则”
template <class op1,class op2,class op3>
class compose_f_gxy_t :public unary_function<typename op1::result_type, typename op2::argument_type>
{
private:
	op1 myop1;
	op2 myop2;
	op3 myop3;
public:
	compose_f_gxy_t(const op1 &o1, const op2 &o2, const op3 &o3) :myop1(o1), myop2(o2), myop3(o3){};
	typename op1::result_type operator()(const typename op2::argument_type &x)
	{
		return myop1(myop2(x), myop3(x));
	}
};
template<class op1,class op2,class op3>
inline compose_f_gxy_t<op1, op2, op3> compose_f_gxy(const op1 &o1, const op2&o2, const op3 &o3)
{
	return compose_f_gxy_t<op1, op2, op3>(o1, o2, o3);
}

void main()
{
	vector<float>vt;
	generate_n(back_inserter(vt), 20, sequence(1));//赋值1-20
	copy(vt.begin(), vt.end(), ostream_iterator<float>(cout, ":"));
	cout << endl;
	vector<float>::iterator pr;
	//删掉小于10大于21的元素
	pr = remove_if(vt.begin(), vt.end(), compose_f_gxy(logical_and<bool>(), bind2nd(greater<float>(), 10), bind2nd(less<float>(), 21)));
	vt.erase(pr, vt.end());
	copy(vt.begin(), vt.end(), ostream_iterator<float>(cout, ":"));
	system("pause");
}

2.二元组合函数配接器



#include<iostream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
using namespace std;

template<class op1,class op2,class op3>
class compose_f_gx_hx_t :public binary_function
< typename op1::result_type, typename op2::argument_type, typename op3::argument_type >
{

private:
	op1 myop1;
	op2 myop2;
	op3 myop3;
public:
	compose_f_gx_hx_t(const op1 &o1, const op2 &o2, const op3&o3) :myop1(o1), myop2(o2), myop3(o3){};
	typename op1::result_type operator()(const typename op2::argument_type&x, const typename op3::argument_type&y)
	{
		return myop1(myop2(x), myop3(y));
	}
};

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 main()
{
	string s = "Iternationlization";
	string b = "nation";
	string::iterator pr;
	//在指定字符串中搜索子串的功能
	pr = search(s.begin(), s.end(), b.begin(), b.end(), compose_f_gx_hx(equal_to<int>()
		, ptr_fun(::toupper), ptr_fun(::toupper)));
	if (pr != s.end())
	{
		cout << " " << b << "is the part of " << s << endl;
	}
	system("pause");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值