STL 函数适配器

15 篇文章 0 订阅
一、函数对象头文件 :functional


二、函数对象的基类


函数对象就是实现了operator()的对象。在STL中大部分函数对象都是一元函数(一个参数)和二元函数(两个参数).因此STL定义了这两种函数的基类:


//_A:  参数类型,注意这里不是引用,而就是一个具体的类型
//_R:  函数返回类型
template<class _A, class _R>
struct unary_function {
typedef _A argument_type;
typedef _R result_type;
};
// TEMPLATE STRUCT binary_function
template<class _A1, class _A2, class _R>
struct binary_function {
typedef _A1 first_argument_type;
typedef _A2 second_argument_type;
typedef _R result_type;
};




三、预定义函数对象:




template<class _Ty>           //加
struct plus : binary_function<_Ty, _Ty, _Ty> {
_Ty operator()(const _Ty& _X, const _Ty& _Y) const
{return (_X + _Y); }
};
// TEMPLATE STRUCT minus
template<class _Ty>   //减
struct minus : binary_function<_Ty, _Ty, _Ty> {
_Ty operator()(const _Ty& _X, const _Ty& _Y) const
{return (_X - _Y); }
};
// TEMPLATE STRUCT multiplies
template<class _Ty>   //乘
struct multiplies : binary_function<_Ty, _Ty, _Ty> {
_Ty operator()(const _Ty& _X, const _Ty& _Y) const
{return (_X * _Y); }
};
// TEMPLATE STRUCT divides
template<class _Ty>   //除
struct divides : binary_function<_Ty, _Ty, _Ty> {
_Ty operator()(const _Ty& _X, const _Ty& _Y) const
{return (_X / _Y); }
};
// TEMPLATE STRUCT modulus
template<class _Ty>    //模
struct modulus : binary_function<_Ty, _Ty, _Ty> {
_Ty operator()(const _Ty& _X, const _Ty& _Y) const
{return (_X % _Y); }
};
// TEMPLATE STRUCT negate
template<class _Ty>    //取负
struct negate : unary_function<_Ty, _Ty> {
_Ty operator()(const _Ty& _X) const
{return (-_X); }
};
// TEMPLATE STRUCT equal_to
template<class _Ty>     //等于
struct equal_to : binary_function<_Ty, _Ty, bool> {
bool operator()(const _Ty& _X, const _Ty& _Y) const
{return (_X == _Y); }
};
// TEMPLATE STRUCT not_equal_to
template<class _Ty>    //不等于
struct not_equal_to : binary_function<_Ty, _Ty, bool> {
bool operator()(const _Ty& _X, const _Ty& _Y) const
{return (_X != _Y); }
};
// TEMPLATE STRUCT greater
template<class _Ty>    //大于
struct greater : binary_function<_Ty, _Ty, bool> {
bool operator()(const _Ty& _X, const _Ty& _Y) const
{return (_X > _Y); }
};
// TEMPLATE STRUCT less
template<class _Ty>   //小于
struct less : binary_function<_Ty, _Ty, bool> {
bool operator()(const _Ty& _X, const _Ty& _Y) const
{return (_X < _Y); }
};
// TEMPLATE STRUCT greater_equal
template<class _Ty>     //大于等于
struct greater_equal : binary_function<_Ty, _Ty, bool> {
bool operator()(const _Ty& _X, const _Ty& _Y) const
{return (_X >= _Y); }
};
// TEMPLATE STRUCT less_equal
template<class _Ty>   //小于等于
struct less_equal : binary_function<_Ty, _Ty, bool> {
bool operator()(const _Ty& _X, const _Ty& _Y) const
{return (_X <= _Y); }
};
// TEMPLATE STRUCT logical_and
template<class _Ty>     //与,这要求两个类型进行与操作后是BOOL
struct logical_and : binary_function<_Ty, _Ty, bool> {
bool operator()(const _Ty& _X, const _Ty& _Y) const
{return (_X && _Y); }
};
// TEMPLATE STRUCT logical_or
template<class _Ty>  //或
struct logical_or : binary_function<_Ty, _Ty, bool> {
bool operator()(const _Ty& _X, const _Ty& _Y) const
{return (_X || _Y); }
};
// TEMPLATE STRUCT logical_not
template<class _Ty>      //非
struct logical_not : unary_function<_Ty, bool> {
bool operator()(const _Ty& _X) const
{return (!_X); }
};






四、函数对象绑定器。
   函数对象绑定器又叫函数对象适配器,也是一种函数对象。
有时候在算法中需要一个一元函数对象,且这个一元函数对象又类似于某个二元函数固定某个参数的调用。这时候就可以用函数绑定器来将某个二元函数对象转化为一元函数来调用,根据固定的参数则可以分为固定第一个参数和第二个参数,他们分别是:
//固定第一个参数:


//_Bfn : 是一个二元函数对象
template<class _Bfn>
class binder1st
: public unary_function<_Bfn::second_argument_type,
_Bfn::result_type> {
public:
binder1st(const _Bfn& _X,
const _Bfn::first_argument_type& _Y)     //构造函数中必须指出函数对象和第一个参数的值
: op(_X), value(_Y) {}
result_type operator()(const argument_type& _X) const      //这里的第二个参数将是变化的.
{return (op(value, _X)); }
protected:
_Bfn op;
_Bfn::first_argument_type value;
};




为了快速构造出一个绑定对象,可以调用函数:


template<class _Bfn, class _Ty> inline
binder1st<_Bfn> bind1st(const _Bfn& _X, const _Ty& _Y)    //这个函数返回一个一元函数对象,并接收二元函数对象,和第一个参数的固定值.
{return (binder1st<_Bfn>(_X,
_Bfn::first_argument_type(_Y))); }




例如:要查找某个vector中大于10的第一个位置:


find_if(vec.begin(), vec.end(), bind1st(less<int>(), 10));




//而要固定第二个元素则用:
template<class _Bfn>
class binder2nd
: public unary_function<_Bfn::first_argument_type,
_Bfn::result_type> {
public:
binder2nd(const _Bfn& _X,
const _Bfn::second_argument_type& _Y)
: op(_X), value(_Y) {}
result_type operator()(const argument_type& _X) const
{return (op(_X, value)); }
protected:
_Bfn op;
_Bfn::second_argument_type value;
};
// TEMPLATE FUNCTION bind2nd
template<class _Bfn, class _Ty> inline
binder2nd<_Bfn> bind2nd(const _Bfn& _X, const _Ty& _Y)
{return (binder2nd<_Bfn>(_X,
_Bfn::second_argument_type(_Y))); }










//_A 参数类型
//_R 返回类型
//这里的函数适配器接收的是一个函数指针,
//这里支持单参数函数和双参数函数
//这里的函数的调用约定必须是  __cdecl方式
//


template<class _A, class _R>
class pointer_to_unary_function
: public unary_function<_A, _R> {
public:
explicit pointer_to_unary_function(_R (__cdecl *_X)(_A))
: _Fn(_X) {}
_R operator()(_A _X) const
{return (_Fn(_X)); }
protected:
_R (__cdecl *_Fn)(_A);
};
// TEMPLATE CLASS pointer_to_binary_function
template<class _A1, class _A2, class _R>
class pointer_to_binary_function
: public binary_function<_A1, _A2, _R> {
public:
explicit pointer_to_binary_function(
_R (__cdecl *_X)(_A1, _A2))
: _Fn(_X) {}
_R operator()(_A1 _X, _A2 _Y) const
{return (_Fn(_X, _Y)); }
protected:
_R (__cdecl *_Fn)(_A1, _A2);
};
// TEMPLATE FUNCTION ptr_fun


//为了生成一个函数指针适配对象,可以调用函数ptr_fun来实现
template<class _A, class _R> inline
pointer_to_unary_function<_A, _R>
ptr_fun(_R (__cdecl *_X)(_A))
{return (pointer_to_unary_function<_A, _R>(_X)); }
template<class _A1, class _A2, class _R> inline
pointer_to_binary_function<_A1, _A2, _R>
ptr_fun(_R (__cdecl *_X)(_A1, _A2))
{return (pointer_to_binary_function<_A1, _A2, _R>(_X)); }
// TEMPLATE CLASS mem_fun_t




//有时候我们的容器中的元素是对象,而我们又希望算法中的函数对象是类的成员函数.这可以使用如下适配器




//这个适配器是保存一个没有参数的对象数据成员


template<class _R, class _Ty>
class mem_fun_t : public unary_function<_Ty *, _R> {
public:
explicit mem_fun_t(_R (_Ty::*_Pm)())
: _Ptr(_Pm) {}
_R operator()(_Ty *_P) const
{return ((_P->*_Ptr)()); }
private:
_R (_Ty::*_Ptr)();
};
// TEMPLATE FUNCTION mem_fun


//mem_fun用来生成一个适配器
template<class _R, class _Ty> inline
mem_fun_t<_R, _Ty> mem_fun(_R (_Ty::*_Pm)())
{return (mem_fun_t<_R, _Ty>(_Pm)); }
// TEMPLATE CLASS mem_fun1_t




//调用带一个参数的数据成员. 那么在算法中另外一个参数.则是数据成员函数的第一个参数
template<class _R, class _Ty, class _A>
class mem_fun1_t : public binary_function<_Ty *, _A, _R> {
public:
explicit mem_fun1_t(_R (_Ty::*_Pm)(_A))
: _Ptr(_Pm) {}
_R operator()(_Ty *_P, _A _Arg) const
{return ((_P->*_Ptr)(_Arg)); }
private:
_R (_Ty::*_Ptr)(_A);
};
// TEMPLATE FUNCTION mem_fun1


//mem_fun1来生成适配器
template<class _R, class _Ty, class _A> inline
mem_fun1_t<_R, _Ty, _A> mem_fun1(_R (_Ty::*_Pm)(_A))
{return (mem_fun1_t<_R, _Ty, _A>(_Pm)); }
// TEMPLATE CLASS mem_fun_ref_t




//前面的适配器都要求容器的元素是对象指针.而这个适配器则是用在元素是对象的容器上


template<class _R, class _Ty>
class mem_fun_ref_t : public unary_function<_Ty, _R> {
public:
explicit mem_fun_ref_t(_R (_Ty::*_Pm)())
: _Ptr(_Pm) {}
_R operator()(_Ty& _X) const
{return ((_X.*_Ptr)()); }
private:
_R (_Ty::*_Ptr)();
};
// TEMPLATE FUNCTION mem_fun_ref


//mem_fun_ref是用在建立引用的适配器上的
template<class _R, class _Ty> inline
mem_fun_ref_t<_R, _Ty> mem_fun_ref(_R (_Ty::*_Pm)())
{return (mem_fun_ref_t<_R, _Ty>(_Pm)); }
// TEMPLATE CLASS mem_fun1_ref_t
template<class _R, class _Ty, class _A>
class mem_fun1_ref_t : public binary_function<_Ty *, _A, _R> {
public:
explicit mem_fun1_ref_t(_R (_Ty::*_Pm)(_A))
: _Ptr(_Pm) {}
_R operator()(_Ty& _X, _A _Arg) const
{return ((_X.*_Ptr)(_Arg)); }
private:
_R (_Ty::*_Ptr)(_A);
};
// TEMPLATE FUNCTION mem_fun1_ref
template<class _R, class _Ty, class _A> inline
mem_fun1_ref_t<_R, _Ty, _A> mem_fun1_ref(_R (_Ty::*_Pm)(_A))
{return (mem_fun1_ref_t<_R, _Ty, _A>(_Pm)); }






//有时候我们对一个返回bool的函数对象,得到他的非值的函数对象,这可以用适配器




//单元素的非函数适配器
// TEMPLATE CLASS unary_negate
template<class _Ufn>
class unary_negate
: public unary_function<_Ufn::argument_type, bool> {
public:
explicit unary_negate(const _Ufn& _X)
: _Fn(_X) {}
bool operator()(const _Ufn::argument_type& _X) const
{return (!_Fn(_X)); }
protected:
_Ufn _Fn;
};
// TEMPLATE FUNCTION not1
template<class _Ufn> inline
unary_negate<_Ufn> not1(const _Ufn& _X)        //生成适配对象的函数
{return (unary_negate<_Ufn>(_X)); }
// TEMPLATE CLASS binary_negate


//双元素的非函数适配器


template<class _Bfn>
class binary_negate
: public binary_function<_Bfn::first_argument_type,
_Bfn::second_argument_type, bool> {
public:
explicit binary_negate(const _Bfn& _X)
: _Fn(_X) {}
bool operator()(const _Bfn::first_argument_type& _X,
const _Bfn::second_argument_type& _Y) const
{return (!_Fn(_X, _Y)); }
protected:
_Bfn _Fn;
};
// TEMPLATE FUNCTION not2
template<class _Bfn> inline 
binary_negate<_Bfn> not2(const _Bfn& _X)        //生成适配对象的函数
{return (binary_negate<_Bfn>(_X)); }





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值