//---------------------------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. c#中的数据类型简介(委托)

    什么是委托? 委托是一种类型,它封装了一类方法,这些方法具有相同的方法签名(signature)和返回类型.定义听起来有点拗口,首先可以确定委托是一种数据类型,那么什么是方法签名,其实就是指方法的输入 ...

  2. 误删mysql表物理文件的解决方法(不涉及恢复数据)

    该方法只介绍了如何救回这个表名(数据不恢复) 如果想要恢复原来数据 直接用extundelete把文件恢复后放回去即可 并且是适用于平时没有全备的情况下  如果有全备 直接那全备的frm和idb文件放 ...

  3. Eclipse无法自动编译生成class文件

    在Maven项目中,通常eclipse生成的class文件都会在target/classes文件夹下,但是有时候由于各种原因,classes下没有生成class文件,导致项目启动失败 . 大部分cla ...

  4. 【转】Java学习---解析Java Servlet工作过程

    [原文]https://www.toutiao.com/i6594316694657696264/ 解析Java Servlet工作过程 Servlet简介 Servlet是sun公司提供的一门用于开 ...

  5. ConcurrentModificationException 集合并发修改异常 解决

    import java.util.ArrayList; import java.util.List; import java.util.ListIterator; /** * 问题? * 有一个集合, ...

  6. pt-heartbeat工具监控MySQL复制延迟

    pt-heartbeat工作原理: 1,在主库上的某个数据库A中创建一张heartbeat表,按照一定的时间频率更新该表的字段(把时间更新进去). 2,从主库连接到从上的这个数据库A中检查复制的时间记 ...

  7. October 17th 2017 Week 42nd Tuesday

    We execuse our sloth under the pretext of difficulty. 我们常以困难为由,作为懒惰的借口. The process of my system-tra ...

  8. Django商城项目笔记No.18商品部分-数据表创建

    数据库表设计 在电商中对于商品,有两个重要的概念:SPU和SKU SPU = Standard Product Unit (标准产品单位) SPU是商品信息聚合的最小单位,是一组可服用.易检索的标准化 ...

  9. 打开word文档时提示“Microsoft Office Word已停止工作”

    我的电脑(Win10)有Office 2003和2013两个版本,可能由于之前超长待机等原因导致word 2003的文件(.doc)不能正常打开,没次都会提示“Microsoft Office Wor ...

  10. C内存开辟与平移