Generic Programming and the STL笔记6--Function Object Classes

来源:互联网 发布:实时电脑远程软件 编辑:程序博客网 时间:2024/05/22 04:45
1 Function Object Base Classes
1.1 unary_function
unary_function<Arg, Result>


1.2 binary_function
binary_function<Arg1, Arg2, Result>


2 算术运算
2.1 plus
plus<T>


2.2 minus
minus<T>


2.3 multipies
multiplies<T>


2.4 divides
divides<T>


2.5 modulus
modulus<T>


2.6 negate
negate<T>


3 大小比较
3.1 equal_to
equal_to<T>


3.2 not_equal_to
not_equal_to<T>


3.3 less
less<T>


3.4 greater
greater<T>


3.4 less_equal
less_equal<T>


3.5 greater_equal
greater_equal<T>


4 逻辑运算
4.1 logical_and
logical_and<T>


4.2 logical_or
logical_or<T>


4.3 logical_not
logical_not<T>


5 特殊的Function Objects
5.1 hash
hash<T>
类型只针对char *, const char *, string及内建整数。其它是必须定定义template specialization。


6 Member Function Adapters
用于将member functions当作function objects来调用
mem_fun_t<R, X>
mem_fun_ref_t<R, X>
mem_fun1_t<R, X, A>
mem_fun1_ref_t<R, X, A>
const_mem_fun_t<R, X>
const_mem_fun_ref_t<R, X>
const_mem_fun1_t<R, X, A>
const_mem_fun1_ref_t<R, X, A>


template <class S, class T> mem_fun_t<S,T> mem_fun (S (T::*f)());
template <class S, class T, class A> mem_fun1_t<S,T,A> mem_fun (S (T::*f)(A));
template <class S, class T> const_mem_fun_t<S,T> mem_fun (S (T::*f)() const);
template <class S, class T, class A> const_mem_fun1_t<S,T,A> mem_fun (S (T::*f)(A) const);


template <class S, class T>
  mem_fun_ref_t<S,T> mem_fun_ref (S (T::*f)());
template <class S, class T, class A>
  mem_fun1_ref_t<S,T,A> mem_fun_ref (S (T::*f)(A));
template <class S, class T>
  const_mem_fun_ref_t<S,T> mem_fun_ref (S (T::*f)() const);
template <class S, class T, class A>
  const_mem_fun1_ref_t<S,T,A> mem_fun_ref (S (T::*f)(A) const);


用于算法
R mem_fun_t::operator()(X *x) const
会调用x->f(), f()为其构造所接受的参数
R mem_fun_ref_t::operator()(X &x) const
会调用x.f(), f()为其构造所接受的参数
R mem_fun1_t::operator()(X *x, A a) const
会调用x->f(a), f()为其构造所接受的参数
R mem_fun1_ref_t::operator()(X &x, A a) const
会调用x.f(a), f()为其构造所接受的参数


7 其它的Adapters
7.1 bind
binder1st<BinaryFun>
binder2nd<BinaryFun>
bind<class Fn, class ... Args>
如bind(greater<int>(), std::placeholders::_1, 5)
辅助函数
template <class BinaryFun, class T>
binder1st<BinaryFun> bind1st(const BinaryFun &F, const T &c)
template <class BinaryFun, class T>
binder2nd<BinaryFun> bind1st(const BinaryFun &F, const T &c)


用于函数
result_type binder1st::operator()(const argument_type &x) const
即调用F(c, x)
result_type binder1st::operator()(const argument_type &x) const
即调用F(x, c)


7.2 函数指针转换为函数对象
主要用于确定函数类型,当有多个重载函数。把函数指针转换为函数对象。
pointer_to_unary_function<Arg, Result>
pointer_to_binary_function<Arg1, Arg2, Result>


辅助函数
template <class Arg, class Result>
poiter_to_unary_function<Arg, Result> ptr_fun(Result (*x)(Arg))
template <class Arg1, class Arg2, class Result>
poiter_to_binary_function<Arg1, Arg2, Result> ptr_fun(Result (*x)(Arg1, Arg2))


7.3 逻辑负值
unary_negate<UnaryPredicate>
binary_negate<BinaryPredicate>


辅助函数
template <class Predicate>
unary_negate<Predicate> not1(const Predicate &p)
template <class BinaryPredicate>
binary_negate<BinaryPredicate> not2(const BinaryPredicate &p)
0 0
原创粉丝点击