码迷,mamicode.com
首页 > 其他好文 > 详细

stl源码剖析 详细学习笔记 配接器

时间:2015-04-04 09:21:44      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:c++   stl   stl源码剖析   adapter   笔记   




//---------------------------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 iteratorsiostream 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 :包括bindnegatecomposeptr funmem 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(0){}

    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源码剖析 详细学习笔记 配接器

标签:c++   stl   stl源码剖析   adapter   笔记   

原文地址:http://blog.csdn.net/boydfd/article/details/44858259

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!