zoukankan      html  css  js  c++  java
  • 仿函数进阶——组合型仿函数和自定可以使用函数配接器的仿函数

    让自定仿函数也可以使用函数配接器:

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

    一元:

            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"



    
  • 相关阅读:
    vitualBox上建虚拟机centos7
    spring整合Quartz
    Quartz(基础)
    log4j+coomons.logging打印日志
    ssm中使用pagehelper
    码云不必每次都输入用户名和密码的方法
    idea实用的操作
    Iterator
    Search for a Range 解答
    Search in Rotated Sorted Array (I, II) 解答
  • 原文地址:https://www.cnblogs.com/ggzone/p/4052428.html
Copyright © 2011-2022 走看看