总结

  1. 栈 - 先进后出
  2. 底层实现默认是在 deque 的基础上进行封装,可以自行指定容器类型
  3. 不支持遍历行为,因此没有迭代器,主要就 pop 和 push 两个行为,top返回首元素的引用
  4. 初始化时要么初始化为空栈,要么就需要使用对应的底层容器进行初始化(默认是deque)
  5. 注意没有begin和end

用例

  1. std::stack<int> myStack1; // 空栈
  2. std::deque<int> myDeque{1, 2, 3};
  3. std::stack<int> myStack2(myDeque); // 使用deque初始化
  4. std::vector<int> myVector{1, 2, 3};
  5. std::stack<int, std::vector<int>> myStack3(myVector); // 指定底层容器为vector

stack结构

  1. #ifndef __SGI_STL_INTERNAL_STACK_H
  2. #define __SGI_STL_INTERNAL_STACK_H
  3. #include <sequence_concepts.h>
  4. __STL_BEGIN_NAMESPACE
  5. // Forward declarations of operators == and <, needed for friend declaration.//这里默认的底层容器类型是deque容器
  6. template <class _Tp,
  7. class _Sequence __STL_DEPENDENT_DEFAULT_TMPL(deque<_Tp>) >
  8. class stack;
  9. template <class _Tp, class _Seq>
  10. bool operator==(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y);
  11. template <class _Tp, class _Seq>
  12. bool operator<(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y);
  13. template <class _Tp, class _Sequence>
  14. class stack {
  15. // requirements:
  16. __STL_CLASS_REQUIRES(_Tp, _Assignable);
  17. __STL_CLASS_REQUIRES(_Sequence, _BackInsertionSequence);
  18. typedef typename _Sequence::value_type _Sequence_value_type;
  19. __STL_CLASS_REQUIRES_SAME_TYPE(_Tp, _Sequence_value_type);
  20. #ifdef __STL_MEMBER_TEMPLATES
  21. template <class _Tp1, class _Seq1>
  22. friend bool operator== (const stack<_Tp1, _Seq1>&,
  23. const stack<_Tp1, _Seq1>&);
  24. template <class _Tp1, class _Seq1>
  25. friend bool operator< (const stack<_Tp1, _Seq1>&,
  26. const stack<_Tp1, _Seq1>&);
  27. #else /* __STL_MEMBER_TEMPLATES */
  28. friend bool __STD_QUALIFIER
  29. operator== __STL_NULL_TMPL_ARGS (const stack&, const stack&);
  30. friend bool __STD_QUALIFIER
  31. operator< __STL_NULL_TMPL_ARGS (const stack&, const stack&);
  32. #endif /* __STL_MEMBER_TEMPLATES */
  33. public:
  34. // 由于stack仅支持对栈顶元素的操作, 所以不定义STL要求的
  35. // pointer, iterator, difference_type
  36. typedef typename _Sequence::value_type value_type;
  37. typedef typename _Sequence::size_type size_type;
  38. typedef _Sequence container_type;
  39. typedef typename _Sequence::reference reference;
  40. typedef typename _Sequence::const_reference const_reference;
  41. protected:
  42. _Sequence c;//底层容器类型,默认为deque容器
  43. public:
  44. //下面对stack的维护完全依赖于底层容器的操作
  45. stack() : c() {}
  46. explicit stack(const _Sequence& __s) : c(__s) {}
  47. //判断容器是否为空
  48. bool empty() const { return c.empty(); }
  49. //获取容器的大小,即容器中元素的个数
  50. size_type size() const { return c.size(); }
  51. //返回栈顶元素的引用
  52. reference top() { return c.back(); }
  53. const_reference top() const { return c.back(); }
  54. //在栈顶追加元素
  55. void push(const value_type& __x) { c.push_back(__x); }
  56. //弹出栈顶的元素,但不返回任何内容
  57. void pop() { c.pop_back(); }};
  58. //下面是依赖于底层容器的操作运算符
  59. template <class _Tp, class _Seq>
  60. bool operator==(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y){
  61. return __x.c == __y.c;}
  62. template <class _Tp, class _Seq>
  63. bool operator<(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y){
  64. return __x.c < __y.c;}
  65. #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
  66. template <class _Tp, class _Seq>
  67. bool operator!=(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y){
  68. return !(__x == __y);}
  69. template <class _Tp, class _Seq>
  70. bool operator>(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y){
  71. return __y < __x;}
  72. template <class _Tp, class _Seq>
  73. bool operator<=(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y){
  74. return !(__y < __x);}
  75. template <class _Tp, class _Seq>
  76. bool operator>=(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y){
  77. return !(__x < __y);}
  78. #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
  79. __STL_END_NAMESPACE
  80. #endif /* __SGI_STL_INTERNAL_STACK_H */
  81. // Local Variables:// mode:C++// End: