//---------------------------15/04/03----------------------------

/*

配接器概述:

1:adapter是一种设计模式:将一个class的接口转换为另一个class的接口,使得原本因接口不兼容而

不能合作的classes可以一起工作。

2:改变仿函数接口的,称为function adapter,改变容器接口的,称为container adapter,

改变迭代器接口的,称为iterator adapter。

3:container adatper:    queue
, stack

4:iterator adapter :    insert iterators, reverse iterators,iostream iterators。

1>insert iterators:把赋值操作转变成插入操作,有三个相应函数:

back_inserter(Container& x),front_inserter(Container& x),inserter(Container& x);

2>Reverse iterators:把迭代器转变成反向迭代器的adapter;

3>iostream iterators:将迭代器绑定到iostream对象身上,实现相应的输入输出功能。

5:functor adapter :包括bind,negate,compose,ptr fun,mem
fun

辅助函数                       
实际效果               
实际产生的对象

bind1st(const Op& op,           op(x, param);       binder1st<Op>

const T& x);                                (op, arg1_type(x))

bind2nd(const Op& op,           op(param, x);       binder2nd<Op>

const T& x);                               (op, arg2_type(x))

not1(const Pred& pred);         !pred(param);       unary_negate<Pred>(pred)

not2(const Pred& pred);         !pred(param1,       binary_negate<Pred>(pred)

param2);

compose1(const Op1& op1,        op1(op2(param));    unary_compose<Op1,Op2>

const Op2& op2);                            (op1, op2)

compose1(const Op1& op1,        op1(op2(param),     unary_compose<Op1,Op2>

const Op2& op2,             op3(param))     (op1, op2, op3)

const Op3& op3);

ptr_fun(Result(*fp)(Arg));      fp(param);          pointer_to_unary_function

<Arg, Result>(fp)

ptr_fun(Result(*fp)             fp(param1,param2);  pointer_to_binary_function

(Arg1,Arg2)                                 <Arg1, Arg2, Result>(fp)

mem_fun(S (T::*f)());           (param->*f)();      mem_fun_t<S, T>(f)

mem_fun(S (T::*f)() const);     (param->*f)();      const_mem_fun_t<S, T>(f)

mem_fun_ref(S (T::*f)());       (param.*f)();       mem_fun_ref_t<S, T>(f)

mem_fun_ref(S (T::*f)()         (param.*f)();       const_mem_fun_ref_t<S, T>(f)

const);

mem_fun1(S (T::*f)(A));         (param->*f)(x);     mem_fun1_t<S, T, A>(f)

mem_fun1(S (T::*f)(A) const);   (param->*f)(x);     const_mem_fun1_t<S, T, A>(f)

mem_fun1_ref(S (T::*f)(A));     (param.*f)(x);      mem_fun1_ref_t<S, T, A>(f)

mem_fun1_ref(S (T::*f)(A)       (param.*f)(x);      const_mem_fun1_ref_t<S, T, A>(f)

const);

*/

//back_insert_iterator

template<class Container>

class back_insert_iterator

{

protected:

Container* container;

public:

typedef output_iterator_tag iterator_category;

typedef void                value_type;

typedef void                difference_type;

typedef void                pointer;

typedef void                reference;

explicit back_insert_iterator(Container& x) : container(&x) {}

//唯一要注意的点,把赋值操作改成了push_back操作

back_insert_iterator<Container>&

operator=(const
typename Container::value_type& value)

{

container->push_back(value);

return *this;

}

//禁止* ++的操作

back_insert_iterator<Container>&
operator*() {return *this;}

back_insert_iterator<Container>&
operator++() {return *this;}

back_insert_iterator<Container>&
operator++(int) {return *this;}

};

//辅助函数,用来返回一个back_insert_iterator对象,使用辅助函数可以使的使用变简单。

template<class Container>

inline back_insert_iterator<Container> back_insert(Container& x)

{

return back_insert_iterator<Container>(x);

}

//front_insert_iterator

template<class Container>

class front_insert_iterator

{

protected:

Container* container;

public:

typedef output_iterator_tag iterator_category;

typedef void                value_type;

typedef void                difference_type;

typedef void                pointer;

typedef void                reference;

explicit front_insert_iterator(Container& x) : container(&x) {}

//注意,迭代器指向的容器要有push_front操作才行

front_insert_iterator<Container>&

operator=(const
typename Container::value_type& value)

{

container->push_front(value);

return *this;

}

//禁止* ++的操作

back_insert_iterator<Container>&
operator*() {return *this;}

back_insert_iterator<Container>&
operator++() {return *this;}

back_insert_iterator<Container>&
operator++(int) {return *this;}

};

//辅助函数,用来返回一个front_insert_iterator对象,

template<class Container>

inline front_insert_iterator<Container> front_insert(Container& x)

{

return front_insert_iterator<Container>(x);

}

//insert_iterator

template<class Container>

class front_insert_iterator

{

protected:

Container* container;

typename Container::iterator iter;

public:

typedef output_iterator_tag iterator_category;

typedef void                value_type;

typedef void                difference_type;

typedef void                pointer;

typedef void                reference;

explicit insert_iterator(Container& x,
typename Container::iterator i)

: container(&x), iter(i) {}

insert_iterator<Container>&

operator=(const
typename Container::value_type& value)

{

iter = container->insert(iter, value);

++iter;

return *this;

}

//禁止* ++的操作

insert_iterator<Container>&
operator*() {return *this;}

insert_iterator<Container>&
operator++() {return *this;}

insert_iterator<Container>&
operator++(int) {return *this;}

};

//辅助函数,用来返回一个insert_iterator对象,

template<class Container>

inline insert_iterator<Container> front_insert(Container& x, iterator i)

{

typedef typename Container::iterator iter;

return insert_iterator<Container>(x, iter(i));

}

//reverse_iterator

template<class Iterator>

class reverse_iterator

{

protected:

Iterator current;

public:

typedef typename iterator_traits<Iterator>::iterator_category

iterator_category;

typedef typename iterator_traits<Iterator>::value_type

value_type;

typedef typename iterator_traits<Iterator>::difference_type

difference_type;

typedef typename iterator_traits<Iterator>::pointer

pointer;

typedef typename iterator_traits<Iterator>::reference

reference;

typedef Iterator iterator_type;

typedef reverse_iterator<Iterator> self;

public:

reverse_iterator(){}

explicit reverse_iterator(iterator_type x): current(x) {}

reverse_iterator(const self& x): current(x.current) {}

iterator_type base()
const { return current; }

reference
operator*() const

{

Iterator tmp = current;

return *--tmp;

}

pointer
operator->() const {
return &(operator*());}

self&
operator++()

{

--current;

return *this;

}

self
operator++(int)

{

self tmp = *this;

--current;

return tmp;

}

self&
operator--()

{

++current;

return *self;

}

self
operator--(int)

{

Iterator tmp = current;

++current;

return tmp;

}

self
operator+(difference_type n)
const

{

return self(current - n);

}

self&
operator+=(difference_type n)

{

current -= n;

return *this;

}

self
operator-(difference_type n)
const

{

return self(current + n);

}

self&
operator-=(difference_type n)

{

current +=n;

return *this;

}

//第二个this

指向 自己的指针,第二个星号不会调用operator*,

//*this是一个reverse_iterator对象,+

第一个* 都会调用operator操作

reference
operator[](difference_type n)
const

{

return *(*this + n);

}

};

//stream iterators

//istream_iterator

template<class T,
class Distance = ptrdiff_t>

class istream_iterator

{

firend
bool

operator== __STL_NULL_TMPL_ARGS(const istream_iterator<T, Distance>& x,

const istream_iterator<T, Distance>& y);

protected:

istream* stream;

T value;

bool end_marker;//输入结束标识

void read()

{

//看stream能否读取到数据,能就设置true,否则设置false

end_marker = (*stream) ?
true : false;

if(end_marker)

*stream >> value;

end_marker = (*stream) ?
true : false;

}

public:

typedef input_iterator_tag  iterator_category;

typedef T                   value_type;

typedef Distance            difference_type;

typedef const T*            pointer;

typedef const T&            reference;

//默认是cin,不会读数据

istream_iterator(): stream(&cin), end_marker(false){}

//如果自己传入一个istream
会马上调用read(),然后就会读数据了

istream_iterator(istream& s): stream(&s) {read();}

reference
operator*() const {
return value;}

pointer
operator->() const {
return &(operator*());}

istream_iterator<T, Distance>&
operator++()

{

read();

return *this;

}

istream_iterator<T, Distance>
operator++(int)

{

istream_iterator<T, Distance> tmp = *this;

read();

return tmp;

}

};

//ostream_iterator

template<class T>

class ostream_iterator

{

protected:

ostream* stream;

const char* string;

public:

typedef output_iterator_tag iterator_category;

typedef void                value_type;

typedef void                difference_type;

typedef void                pointer;

typedef void                reference;

ostream_iterator(ostream& s):stream(&s), string(){}

ostream_iterator(ostream& s,
const char* c):stream(&s), string(c) {};

//改写赋值操作为输出到ostream

ostream_iterator<T>&
operator=(const T& value)

{

*stream << value;

if(string)

*stream << string;

return *this;

}

ostream_iterator<T>&
operator*() {return *this;}

ostream_iterator<T>&
operator++() {return *this;}

ostream_iterator<T>&
operator++(int) {return *this;}

};

//function adapters

//unary_negate

template<class Predicate>

class unary_negate :
public unary_function<typename Predicate::

argument_type,
bool>

{

protected:

Predicate pred;

public:

explicit unary_negate(const Predicate& x):pred(x) {}

bool operator()(const
typename Predicate::argument_type& x)
const

{

return !pred(x);

}

};

template<class Predicate>

inline unary_negate<Predicate> not1(const Predicate& pred)

{

return unary_negate<Predicate>(pred);

}

//binary_negate

template<class Predicate>

class binary_negate :
public binary_function

<typename Predicate::first_argument_type,

typename Predicate::second_argument_type,bool>

{

protected:

Predicate pred;

public:

explicit binary_negate(const Predicate& x): pred(x) {}

bool operator()(const
typename Predicate::first_argument_type& x,

const typename Predicate::second_argument_type& y)
const

{

return !pred(x, y);

}

};

template<class Predicate>

inline binary_negate<Predicate> not2(const Predicate& pred)

{

return binary_negate<Predicate>(pred);

}

//binder1st

template<class Operation>

class binder1st:
public unary_function<typename Operation::second_argument_type,

typename Operation::result_type>

//绑定后就变成unary_function了

{

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 T>

inline binder1st<Operation> bind1st(const Operation& op,
const T& x)

{

typedef typename Operation::first_argument_type arg1_type;

return binder1st<Operation>(op, arg1_type(x));

}

//binder2nd

template<class Operation>

class binder2nd :
public unary_function<typename Operation::first_argument_type,

typename Operation::result_type>

{

protected:

Operation op;

typename Operation::second_argument_type value;

public:

binder2nd(const Operation& x,

const typename Operation::second_argument_type& y)

:op(x), value(y) {}

typename Operation::result_type

operator(const
typename Operation::first_argument_type& x)
const

{

return op(x, value);

}

};

template<class Operation,
class T>

inline binder2nd<Operation> bind2nd(const Operation& op,

const T& x)

{

typedef typename Operation::second_argument_type arg2_type;

return binder2nd<Operation>(op, arg2_type(x));

}

//unary_compose

template<class Operation1,
class Operation2>

class unary_compose :
public unary_function<typename Operation2::argument_type,

typename Operation1::result_type>

{

protected:

Operation1 op1;

Operation2 op2;

public:

unary_compose(const Operation1& x,
const Operation2& y): op1(x), op2(y) {}

typename Operation2::result_type

operator()(const
typename Operation2::argument_type& x)

{

return op1(op2(x));

}

};

template<class Operation1,
class Operation2>

inline unary_compose<Operation1, Operation2>

compose1(const Operation1& op1,
const Operation2& op2)

{

return unary_compose<Operation1, Operation2>(op1, op2);

}

//binary_compose

template<class Operation1,
class Operation2,
class Operation3>

class binary_compose :
public unary_function<typename Operation2::argument_type,

typename Operation1::result_type>

{

protected:

Operation1 op1;

Operation2 op2;

Operation3 op3;

public:

binary_compose(const Operation1& x,
const Operation2& y,
const Operation3& z)

:op1(x), op2(y), op3(z) {}

typename Operation1::result_type

operator()(const
typename Operation2::argument_type& x)
const

{

return op1(op2(x), op3(x));

}

};

template<class Operation1,
class Operation2,
class Operation3>

inline binary_compose<Operation1, Operation2, Operation3>

compose2(const Operation1& op1,
const Operation2& op2,
const Operation3& op3)

{

return binary_compose<Operation1, Operation2, Operation3>(op1, op2, op3);

}

//ptr_fun

//pointer_to_unary_function

template<class Arg,
class Result>

class pointer_to_unary_function :
public unary_function<Arg, Result>

{

protected:

Result (*ptr)(Arg);

public:

pointer_to_unary_function(){}

explicit pointer_to_unary_function(Result (*x)(Arg)) : ptr(x) {}

Result
operator()(Arg x)
const {return ptr(x); }

};

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);

}

//pointer_to_binary_function

template<class Arg1,
class Arg2, class Result>

class pointer_to_binary_function :
public binary_function<Arg1, Arg2, Result>

{

protected:

Result (*ptr)(Arg1, Arg2)

public:

pointer_to_binary_function(){}

explicit pointer_to_binary_function(Result (*x)(Arg1, Arg2))

:   ptr(x) {}

Result
operator()(Arg1, Arg2)
const {return ptr(x, y); }

};

template<class Arg1,
class Arg2, class Result>

inline pointer_to_unary_function<Arg1, Arg2, Result>

ptr_fun(Result (*x)(Arg1, Arg2))

{

return pointer_to_binary_function<Arg1, Arg2, Result>(x);

}

//mem_fun

//mem_fun_t

template<class S,
class T>

class mem_fun_t :
public unary_function<T*, S>

{

public:

explicit mem_fun_t(S (T::*pf)) : f(pf) {}

S
operator()(T* p) const {
return (p->*f)(); }

private:

S (T::*f)();

};

//cosnt_mem_fun_t

template<class S,
class T>

class cosnt_mem_fun_t :
public unary_function<const T*, S>

{

public:

explicit cosnt_mem_fun_t(S (T::*pf)
const) : f(pf) {}

S
operator()(const T* p)
const { return (p->*f)(); }

private:

S (T::*f)()
const;

};

//mem_fun_ref_t

template<class S,
class T>

class mem_fun1_ref_t :
public unary_function<T, S>

{

public:

explicit mem_fun1_ref_t(S (T::*pf)()) : f(pf) {}

S
operator()(T& r) {
return (r.*f)();}

private:

S (T::*f)();

};

//const_mem_fun_ref_t

template<class S,
class T>

class const_mem_fun_ref_t :
public unary_function<T, S>

{

public:

const_mem_fun_ref_t(S (T::*pf)()
const) : f(pf) {}

S
operator()(const T& r)
const { return (r.*f)();}

private:

S (T::*f)()
const;

};

//mem_fun1_t

template<class S,
class T, class A>

class mem_fun1_t : binary_function<T*, A, S>

{

public:

mem_fun1_t(S (T::*pf)(A)) : f(pf) {}

S
operator()(T* p, A x) {
return (p->*f)(x); }

private:

S (T::*f)(A);

};

//const_mem_fun1_t

template<class S,
class T, class A>

class const_mem_fun1_t :
public binary_function<const T*, A, S>

{

public:

const_mem_fun1_t(S (T::*pf)
const ) : f(pf) {}

S
operator()(const T* p, A x)
const { return (p->*f)(x); }

private:

S (T::*f)(A)
const;

};

//mem_fun1_ref_t

template<class S,
class T, class A>

class mem_fun1_ref_t : binary_function<T, A, S>

{

public:

mem_fun1_ref_t(S (T::*pf)(A)) : f(pf) {}

S
operator()(T& r, A x) {
return (r.*f)(x); }

private:

S (T::*f)(A);

};

//const_mem_fun1_ref_t

template<class S,
class T, class A>

class const_mem_fun1_ref_t :
public binary_function<T, A, S>

{

public:

const_mem_fun1_ref_t(S (T::*pf)
const ) : f(pf) {}

S
operator()(const T& r, A x)
const { return (r.*f)(x); }

private:

S (T::*f)(A)
const;

};

//各个辅助函数

//fun

template<class S,
class T>

inline mem_fun_t<S,T> mem_fun(S (T::*f)())

{

return mem_fun_t<S,T>(f);

}

template<class S,
class T>

inline const_mem_fun_t<S,T> mem_fun(S (T::*f)()
const)

{

return const_mem_fun_t<S,T>(f);

}

template<class S,
class T>

inline mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)())

{

return mem_fun_ref_t<S,T>(f);

}

template<class S,
class T>

inline const_mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)()
const)

{

return const_mem_fun_ref_t<S,T>(f);

}

//fun1

template<class S,
class T, class A>

inline mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A))

{

return mem_fun1_t<S,T,A>(f);

}

template<class S,
class T, class A>

inline const_mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A)
const)

{

return const_mem_fun1_t<S,T,A>(f);

}

template<class S,
class T, class A>

inline mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A))

{

return mem_fun1_ref_t<S,T,A>(f);

}

template<class S,
class T, class A>

inline const_mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A)
const)

{

return const_mem_fun1_ref_t<S,T,A>(f);

}

stl源码剖析 详细学习笔记 配接器的更多相关文章

  1. stl源码剖析 详细学习笔记 空间配置器

    //---------------------------15/04/05---------------------------- /* 空间配置器概述: 1:new操作包含两个阶段操作 1>调 ...

  2. stl源码剖析 详细学习笔记 仿函数

    //---------------------------15/04/01---------------------------- //仿函数是为了算法而诞生的,可以作为算法的一个参数,来自定义各种操 ...

  3. stl源码剖析 详细学习笔记 hashtable

    //---------------------------15/03/24---------------------------- //hashtable { /* 概述: sgi采用的是开链法完成h ...

  4. stl源码剖析 详细学习笔记 set map

    // //  set map.cpp //  笔记 // //  Created by fam on 15/3/23. // // //---------------------------15/03 ...

  5. stl源码剖析 详细学习笔记 RB_tree (1)

    // //  RB_tree_STL.cpp //  笔记 // //  Created by fam on 15/3/21. // // #include "RB_tree_STL.h&q ...

  6. stl源码剖析 详细学习笔记heap

    // //  heap.cpp //  笔记 // //  Created by fam on 15/3/15. // // //---------------------------15/03/15 ...

  7. stl源码剖析 详细学习笔记 算法(1)

    //---------------------------15/03/27---------------------------- //算法 { /* 质变算法:会改变操作对象之值 所有的stl算法都 ...

  8. stl源码剖析 详细学习笔记 算法总览

    //****************************基本算法***************************** /* stl算法总览,不在stl标准规格的sgi专属算法,都以 *加以标 ...

  9. stl源码剖析 详细学习笔记 RB_tree (2)

    //---------------------------15/03/22---------------------------- //一直好奇KeyOfValue是什么,查了下就是一个和仿函数差不多 ...

随机推荐

  1. MySQL crash-safe replication(3): MySQL的Crash Safe和Binlog的关系

    2016-12-23 17:29 宋利兵 作者:宋利兵 来源:MySQL代码研究(mysqlcode) 0.导读 本文重点介绍了InnoDB的crash safe和binlog之间的关系,以及2阶段提 ...

  2. 斯诺克台球比赛规则 (Snooker)

    斯诺克台球比赛规则 斯诺克(Snooker)的意思是“阻碍.障碍”,所以斯诺克台球有时也被称为障碍台球.此项运动使用的球桌长约3569毫米.宽1778毫米,台面四角以及两长边中心位置各有一个球洞,使用 ...

  3. Resource View Window of Visual Studio

    https://msdn.microsoft.com/en-us/library/d4cfawwc.aspx For the latest documentation on Visual Studio ...

  4. 第六次作业 orm整合 接口

    结合以前一个项目,将普通的jdbc进行了相关整合,全部改写成了hibernate接口 项目名称:短视频分享平台 主要功能:用户模块:注册.登录.编辑资料.查看用户相关 分类模块:分类添加.查看 视频共 ...

  5. MySQL基础之 如何删除主键

    我们在一个表中设置了主键之后,那么如何删除主键呢? 删除主键的语法是: ALTER TABLE TABLE_NAME DROP PRIMARY KEY; 在这里我们要考虑两种情况: 1.可以直接使用d ...

  6. div中嵌套div水平居中,垂直居中

    方法一: div(父):display:table; div(子):display:table_cell;margin:0 auto;vertical-align:middle; 方法二: div(父 ...

  7. 【CSS】Sass理解

    原文在 https://github.com/zhongxia245/blog , 欢迎 star! Sass理解 时间:2016-09-24 22:56:12 作者:zhongxia 这里就不讲解S ...

  8. 团队作业——Beta冲刺2

    团队作业--Beta冲刺 冲刺任务安排 杨光海天 今日任务:根据冲刺内容,具体分配个人任务,对于冲刺内容做准备 明日任务:图片详情界面的开发 吴松青 今日任务:学习熟悉安卓开发,跟随组员快速了解其代码 ...

  9. composer(管理依赖关系的工具) 及配置信息

    Composer 是 PHP 用来管理依赖(dependency)关系的工具.你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer 会帮你安装这些依赖的库文件. 配置文件 ...

  10. 【转】通过blob获取图像并显示

    HTML代码: <div id="forAppend" class="demo"></div> JS代码: var eleAppend ...