函数配接器

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


1.一元组合函数配接器

#include
#include
#include
#include
#include
using namespace std;
//组合“一个运作规则”
template
class compose_f_gx_t :public unary_function
{
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 
inline compose_f_gx_tcompose_f_gx(const op1 &o1, const op2 &o2)
{
	return compose_f_gx_t(o1, o2);
}


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

//组合“两个运作规则”
template 
class compose_f_gxy_t :public unary_function
{
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
inline compose_f_gxy_t compose_f_gxy(const op1 &o1, const op2&o2, const op3 &o3)
{
	return compose_f_gxy_t(o1, o2, o3);
}

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

2.二元组合函数配接器



#include
#include
#include
#include
#include
using namespace std;

template
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
inline compose_f_gx_hx_tcompose_f_gx_hx(const op1 &o1, const op2&o2, const op3 &o3)
{
	return compose_f_gx_hx_t(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()
		, ptr_fun(::toupper), ptr_fun(::toupper)));
	if (pr != s.end())
	{
		cout << " " << b << "is the part of " << s << endl;
	}
	system("pause");
}

你可能感兴趣的:(c++STL中的仿函数,配接器)