函数适配器(function adapter):通过不同函数适配器的绑定,组合和修饰能力,可以实现强大的功能,配合STL泛型算法完成复杂功能。

绑定(bind)

template <class _Operation>
class binder1st
: public unary_function<typename _Operation::second_argument_type,
typename _Operation::result_type> {
protected:
_Operation op;
typename _Operation::first_argument_type value;
public:
binder1st(const _Operation& __x,
const typename _Operation::first_argument_type& __y)
: op(__x), value(__y) {}
typename _Operation::result_type
operator()(const typename _Operation::second_argument_type& __x) const {
return op(value, __x);
}
}; template <class _Operation, class _Tp>
inline binder1st<_Operation>
bind1st(const _Operation& __fn, const _Tp& __x)
{
typedef typename _Operation::first_argument_type _Arg1_type;
return binder1st<_Operation>(__fn, _Arg1_type(__x));
}

bind1st函数有两个参数,被绑定参数的仿函数__fn,以及待绑定到仿函数上的参数值__x。在函数中构建并返回了binder1st对象,并设置了相应的构造参数。

binder1st类中有一个仿函数(函数对象)成员op和待绑定参数value,binder1st构造函数会用传入的仿函数类和待绑定参数来初始化其类成员op与value。

binder1st本身也是一个仿函数类(functor),在类中定义的函数调用操作符(operator())内完成了仿函数的实际功能。该函数有一个参数__x,指定操作的第二参数。然后用调用待绑定参数类op的构造函数,并用value和__x进行初始化。返回的对象的第一个参数就被绑定到binder1st的待绑定参数成员value,以实现参数绑定的功能。

binder1st的原理与binder1st相类似。

否定(not)

template <class _Predicate>
inline unary_negate<_Predicate>
not1(const _Predicate& __pred)
{
return unary_negate<_Predicate>(__pred);
} template <class _Predicate>
class unary_negate
: public unary_function<typename _Predicate::argument_type, bool> {
protected:
_Predicate _M_pred;
public:
explicit unary_negate(const _Predicate& __x) : _M_pred(__x) {}
bool operator()(const typename _Predicate::argument_type& __x) const {
return !_M_pred(__x);
}
};

not1表示一元谓词否定,传入仿函数对象,返回与传入对象的结果相反的一元谓词仿函数对象。

not2表示二元谓词否定,传入仿函数对象,返回与传入对象的结果相反的二元谓词仿函数对象。

合成(compose)

合成的作用类似于数学中的复合函数,分为一元合成和二元合成:

一元合成:h(x)=f(g(x))

二元合成:h(x)=f(g1(x),g2(x))

template <class _Operation1, class _Operation2>
inline unary_compose<_Operation1,_Operation2>
compose1(const _Operation1& __fn1, const _Operation2& __fn2)
{
return unary_compose<_Operation1,_Operation2>(__fn1, __fn2);
} template <class _Operation1, class _Operation2>
class unary_compose
: public unary_function<typename _Operation2::argument_type,
typename _Operation1::result_type>
{
protected:
_Operation1 _M_fn1;
_Operation2 _M_fn2;
public:
unary_compose(const _Operation1& __x, const _Operation2& __y)
: _M_fn1(__x), _M_fn2(__y) {}
typename _Operation1::result_type
operator()(const typename _Operation2::argument_type& __x) const {
return _M_fn1(_M_fn2(__x)); //在此处发挥作用
}
};

二元合成的原理和一元合成相类型。

  binary_compose(const _Operation1& __x, const _Operation2& __y,
const _Operation3& __z)
: _M_fn1(__x), _M_fn2(__y), _M_fn3(__z) { }
typename _Operation1::result_type
operator()(const typename _Operation2::argument_type& __x) const {
return _M_fn1(_M_fn2(__x), _M_fn3(__x));
}

函数指针(ptr_mem)

这种配接器使我们能够将一般函数当作仿函数使用。

如果不使用这里的函数指针适配器先作一番包装,则一般函数无配接能力,无法和前面介绍的其他配接器接轨。

template <class _Arg, class _Result>
inline pointer_to_unary_function<_Arg, _Result> ptr_fun(_Result (*__x)(_Arg))
{
return pointer_to_unary_function<_Arg, _Result>(__x);
} template <class _Arg, class _Result>
class pointer_to_unary_function : public unary_function<_Arg, _Result> {
protected:
_Result (*_M_ptr)(_Arg);
public:
pointer_to_unary_function() {}
explicit pointer_to_unary_function(_Result (*__x)(_Arg)) : _M_ptr(__x) {}
_Result operator()(_Arg __x) const { return _M_ptr(__x); }
};

成员函数指针适配器(mem_fun,mem_fun_ref)

这类适配器可将成员函数作为仿函数来使用。

当容器中存放的是类的指针或者引用类型时,利用泛型算法对容器中元素进行处理时,便可使用成员函数指针适配器动态调用类中定义的虚函数,从而实现多态。

成员函数指针适配器按照参数个数,通过引用还是指针调用,以及是否为静态成员函数,可以分为8种:

函数名称 特征
mem_fun(S (T::*f)()) 无参数;pointer;non-const
mem_fun1(S (T::*f)(A)) 有参数;pointer;non-const
mem_fun(S (T::*f)() const) 无参数;pointer;const
mem_fun1(S (T::*f)(A) const) 有参数;pointer;const
mem_fun_ref(S (T::*f)()) 无参数;reference;non-const
mem_fun1_ref(S (T::*f)(A)) 有参数;reference;non-const
mem_fun_ref(S (T::*f)() const) 无参数;reference;const
mem_fun1_ref(S (T::*f)(A) const) 有参数;reference;const

STL 函数适配器(function adapter)的更多相关文章

  1. STL函数适配器

    一:适配器简介 C++中有三类适配器,分别是容器适配器,迭代器适配器和函数适配器,这里主要介绍函数适配器. (一)函数适配器简介 STL中已经定义了大量的函数对象,但是有时候需要对函数返回值进行进一步 ...

  2. C++ 11 - STL - 函数对象(Function Object) (上)

    1. 定义 在STL中,可以把函数传递给算法,也可以把函数对象传递给算法. 那么,什么是函数对象呢? 我们来看下它的声明: class X { public: // define function c ...

  3. C++ 11 - STL - 函数对象(Function Object) (下)

    1. 预定义函数对象 C++标准库内含许多预定义的函数对象,也就是内置的函数对象. 你可以充分利用他们,不必自己费心去写一些自己的函数对象. 要使用他们,你只要包含如下头文件 #include < ...

  4. C++ 11 - STL - 函数对象(Function Object) (中)

    我们再来看一个复杂的例子 需求: 我们需要对集合内每个元素加上一个特定的值 代码如下: AddInt.h class AddInt { private: int theValue; // the va ...

  5. function adapter(函数适配器)和迭代器适配器

    所谓function adapter(函数适配器)是指能够将不同的函数对象(或是和某值或某寻常函数)结合起来的东西,它自身也是个函数对象. 迭代器适配器  运用STL中的迭代器适配器,可以使得算法能够 ...

  6. C++STL 预定义函数对象和函数适配器

    预定义函数对象和函数适配器 预定义函数对象基本概念:标准模板库STL提前定义了很多预定义函数对象,#include <functional> 必须包含. 1使用预定义函数对象: void ...

  7. C++11之function模板和bind函数适配器

    在C++98中,可以使用函数指针,调用函数,可以参考之前的一篇文章:类的成员函数指针和mem_fun适配器的用法.   简单的函数调用   对于函数: void foo(const string &a ...

  8. STL算法设计理念 - 函数适配器

    1)函数适配器的理论知识 2)常用函数函数适配器 标准库提供一组函数适配器,用来特殊化或者扩展一元和二元函数对象.常用适配器是: 1.绑定器(binder): binder通过把二元函数对象的一个实参 ...

  9. 函数适配器bind2nd 、mem_fun_ref 源码分析、函数适配器应用举例

    一.适配器 三种类型的适配器: 容器适配器:用来扩展7种基本容器,利用基本容器扩展形成了栈.队列和优先级队列 迭代器适配器:(反向迭代器.插入迭代器.IO流迭代器) 函数适配器:函数适配器能够将仿函数 ...

随机推荐

  1. InnoDB数据库 ibdata1 被删除后 的恢复方法

    前提条件:1  ibdata1 被删除 2  数据库文件还存在  特别是 ibd文件 3  原来数据库表结构及索引还在 恢复步骤: 1. 将原来的数据文件COPY到其它目录下. 2. 创建同名表,表结 ...

  2. springboot 配置quart多数据源

    Springboot版本为2.1.6 多数据源配置使用druid进行配置,数据库使用的为Oracle11g,如果使用的是MySQL,直接将数据库的地址和驱动改一下即可 <parent> & ...

  3. R语言-六大数据结构

    R语言有六种基本的数据结构(或者说数据类型吧).根据数据的维度和同质/异质可分为5种数据类型,最后再介绍一种特殊的类型“因子”.   同质 异质 1维 原子向量 列表 2维 矩阵 数据框 n维 数组 ...

  4. 【NOIP2017提高组模拟12.10】幻魔皇

    题目 幻魔皇拉比艾尔很喜欢斐波那契树,他想找到神奇的节点对. 所谓斐波那契树,根是一个白色节点,每个白色节点都有一个黑色节点儿子,而每个黑色节点则有一个白色和一个黑色节点儿子.神奇的节点对则是指白色节 ...

  5. 伸展树splay之求区间极值

    前言 这篇博客是根据我在打这道题的时候遇到的问题,来打的,有些细节可能考虑不到. 题目 在N(1<=N<=100000)个数A1-An组成的序列上进行M(1<=M<=10000 ...

  6. 【leetcode】1222. Queens That Can Attack the King

    题目如下: On an 8x8 chessboard, there can be multiple Black Queens and one White King. Given an array of ...

  7. Pycharm,出现Invalid VCS root mapping The directory 解决方法

    Pycharm File 中setting-------version control  中VCS选择none  后选择ok 执行完以上的步骤,还错误就会消失.

  8. (66)Nginx+lua+Redis开发

    一. 概述 Nginx是一个高性能,支持高并发的,轻量级的web服务器.目前,Apache依然web服务器中的老大,但是在全球前1000大的web服务器中,Nginx的份额为22.4%.Nginx采用 ...

  9. java语言对比,jvm,垃圾回收

    1.java/c++/ruby/python集中语言的对比 java和c++ 1,没有指针 2,没有多继承 3,没有const 4,在实现多态上的区别    tc++里面的虚函数,纯续函数和java里 ...

  10. Starting MySQL... ERROR! The server quit without updating PID file (/usr/local/mysql/data/VM_0_6_centos.pid)

    刚接触MySql数据库,参考一些文章后搭建起来了也创建了数据库,程序跑到很好,一觉醒来突然连接不上了 MySql数据库了. 研究了好一会才找到原因. 现象: 登录数据库失败 [root@VM_0_6_ ...