zoukankan      html  css  js  c++  java
  • stack

    stack

    template <class _Tp, 
              class _Sequence __STL_DEPENDENT_DEFAULT_TMPL(deque<_Tp>) >
    class stack;
    
    template <class _Tp, class _Seq>
    bool operator==(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y);
    
    template <class _Tp, class _Seq>
    bool operator<(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y);
    
    //@ stack 类,_Sequence 容器,这里默认的底层容器类型是deque容器
    template <class _Tp, class _Sequence>
    class stack {
      // requirements:
      __STL_CLASS_REQUIRES(_Tp, _Assignable);
      __STL_CLASS_REQUIRES(_Sequence, _BackInsertionSequence);
      typedef typename _Sequence::value_type _Sequence_value_type;
      __STL_CLASS_REQUIRES_SAME_TYPE(_Tp, _Sequence_value_type);
    
    
    #ifdef 
    
      template <class _Tp1, class _Seq1>
      friend bool operator== (const stack<_Tp1, _Seq1>&,
                              const stack<_Tp1, _Seq1>&);
      template <class _Tp1, class _Seq1>
      friend bool operator< (const stack<_Tp1, _Seq1>&,
                             const stack<_Tp1, _Seq1>&);
    #else /* __STL_MEMBER_TEMPLATES */
      friend bool __STD_QUALIFIER
      operator== __STL_NULL_TMPL_ARGS (const stack&, const stack&);
      friend bool __STD_QUALIFIER
      operator< __STL_NULL_TMPL_ARGS (const stack&, const stack&);
    #endif /* __STL_MEMBER_TEMPLATES */
    
    public:
      //@ 由于stack仅支持对栈顶元素的操作, 所以不定义STL要求的  pointer, iterator, difference_type 
      typedef typename _Sequence::value_type      value_type;
      typedef typename _Sequence::size_type       size_type;
      typedef          _Sequence                  container_type;
    
      typedef typename _Sequence::reference       reference;
      typedef typename _Sequence::const_reference const_reference;
    protected:
      _Sequence c;  // stack 底部容器
    public:
      stack() : c() {}
      explicit stack(const _Sequence& __s) : c(__s) {}
     
     //@ ... 
    };
    
    

    常用操作

     bool empty() const { return c.empty(); }  //@ 判断 stack 是否为空
     size_type size() const { return c.size(); }  //@ 判断 stack 的大小
     reference top() { 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 _Tp, class _Seq>
    bool operator==(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
    {
      return __x.c == __y.c;
    }
    
    template <class _Tp, class _Seq>
    bool operator<(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
    {
      return __x.c < __y.c;
    }
    
    #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
    
    template <class _Tp, class _Seq>
    bool operator!=(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
    {
      return !(__x == __y);
    }
    
    template <class _Tp, class _Seq>
    bool operator>(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
    {
      return __y < __x;
    }
    
    template <class _Tp, class _Seq>
    bool operator<=(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
    {
      return !(__y < __x);
    }
    
    template <class _Tp, class _Seq>
    bool operator>=(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
    {
      return !(__x < __y);
    }
    

    总结

    • stack 是一种“先进后出”的数据结构,它只能在栈顶对数据进行操作,即只能在栈顶进行新增元素、移除元素、取得最顶端元素。
    • stack 不能进行遍历行为,所以不需要设计自己的迭代器。
    • stack 是基于某种容器作为底部结构的,默认容器是 deque 容器,list 也可以作为其底层容器,例如:
    stack<int, list<int>> istack;
    
  • 相关阅读:
    Bootstrap3基础 thumbnail 圆角类型的div块
    Bootstrap3基础 text-muted/success... 辅助类样式 情景文本颜色
    Bootstrap3基础 text-right/left/center 设置标题右对齐、左对齐、居中
    Bootstrap3基础 table-striped 表格实现隔行换色(浅灰色与白色交替)
    Bootstrap3基础 table-condensed 表格中的单元格紧凑一些
    Bootstrap3基础 table-responsive 响应式表格
    Bootstrap3基础 table-bordered/hover 表格加外边框和鼠标悬停对应行的背景色加深
    Bootstrap3基础 page-header 标题下加分割线
    iOS Swift编程语言
    【强烈推荐】XCODE的插件之王
  • 原文地址:https://www.cnblogs.com/xiaojianliu/p/12602937.html
Copyright © 2011-2022 走看看