适配器

    适配器可以理解成接口转换器,比如我们常用的电源适配器,将220V的电压转成低电压。STL中的适配器可以理解成改造器,好比在其对别人进行了改造,换了一层皮肤,进行了包装。像STL中的stack、queue、priority_queue都是容器适配器。查看他们的源码可以知道,他们功能都是借助其他容器实现的,自己只是做了一层转换。stack和queue底层默认的都是调用deque实现的。priority_queue底层是vector容器根据heap规则实现的。

    stack的结构如下,它是只在一端可进可出,只要将deque的一端封住,就是一个stack结构,stl中stack默认采用的底层实现就是deque

   

  stack允许新增元素、移除元素、取得栈顶元素,但除了最顶端外,没有任何其他方法可以存取stack的其它元素,stack没有遍历行为。

 template<class T, class Sequence=deque<T>>
   class stack{
       friend bool operator== __STL_NULL_TMPL_ARGS(const stack&,const stack&);
       friend bool operator< __STL_NULL_TMPL_ARGS(const stack&,const stack&);
        public:
        typedef typename Sequence::value_type value_type;
        typedef typename Sequence:size_type size_type;
        typedef typename Sequence:reference reference;
        typedef typename Sequence:const_reference const_reference;
protected:
    Sequence c; //底层容器
public:
    bool empty()const{
        return c.empty();
   }
    size_type size() const{
        return c.size();
    }
    reference top() const{
        return c.back();
   }
   const_reference top() const{
        return c.back();
  }
  void push(const value_type& x){
        c.push_back(x);
    }
  void pop(){
        c.pop_back();
  }
  };
 template<class T, class Sequence>
 bool operator==(const stack<T,Sequence>& x, const stack<T, Sequence>& y){
        return x.c==y.c;
 }
template<class T,class Sequence>
bool operator<(const stack<T,Sequence>& x,const stack<T,Sequence>& y){
    return x.c<y.c;
}
  deque的结构如下,它是一个在两端可进行可进可出的容器,非常的灵活。


template<class T, class Sequence=deque<T>>
   class queue{
       friend bool operator== __STL_NULL_TMPL_ARGS(const queue&,const queue&);
       friend bool operator< __STL_NULL_TMPL_ARGS(const queue&,const queue&);
        public:
        typedef typename Sequence::value_type value_type;
        typedef typename Sequence:size_type size_type;
        typedef typename Sequence:reference reference;
        typedef typename Sequence:const_reference const_reference;
protected:
    Sequence c; //底层容器
public:
    bool empty()const{
        return c.empty();
   }
    size_type size() const{
        return c.size();
    }
    reference top() const{
        return c.back();
   }
   const_reference top() const{
        return c.back();
  }
  void push(const value_type& x){
        c.push_back(x);
    }
  void pop(){
        c.pop_back();
  }
  };
 template<class T, class Sequence>
 bool operator==(const stack<T,Sequence>& x, const stack<T, Sequence>& y){
        return x.c==y.c;
 }
template<class T,class Sequence>
bool operator<(const stack<T,Sequence>& x,const stack<T,Sequence>& y){
    return x.c<y.c;
}

  通过源码可以看到,stack和queue底层都是对deque的调用实现的,即Sequence c。像pop、push、empty这些操作都是对deque的包装,它本身并没有实现。所以stack和queue我们称它为适配器,而不是叫做第一类容器。

参考资料: STL源码剖析 --侯捷

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值