![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <sequence_concepts.h> 2 3 __STL_BEGIN_NAMESPACE 4 5 // Forward declarations of operators == and <, needed for friend declaration. 6 7 template <class _Tp, 8 class _Sequence __STL_DEPENDENT_DEFAULT_TMPL(deque<_Tp>) > 9 class stack; 10 11 template <class _Tp, class _Seq> 12 bool operator==(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y); 13 14 template <class _Tp, class _Seq> 15 bool operator<(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y); 16 17 18 template <class _Tp, class _Sequence> 19 class stack { 20 21 // requirements: 22 23 __STL_CLASS_REQUIRES(_Tp, _Assignable); 24 __STL_CLASS_REQUIRES(_Sequence, _BackInsertionSequence); 25 typedef typename _Sequence::value_type _Sequence_value_type; 26 __STL_CLASS_REQUIRES_SAME_TYPE(_Tp, _Sequence_value_type); 27 28 29 #ifdef __STL_MEMBER_TEMPLATES 30 template <class _Tp1, class _Seq1> 31 friend bool operator== (const stack<_Tp1, _Seq1>&, 32 const stack<_Tp1, _Seq1>&); 33 template <class _Tp1, class _Seq1> 34 friend bool operator< (const stack<_Tp1, _Seq1>&, 35 const stack<_Tp1, _Seq1>&); 36 #else /* __STL_MEMBER_TEMPLATES */ 37 friend bool __STD_QUALIFIER 38 operator== __STL_NULL_TMPL_ARGS (const stack&, const stack&); 39 friend bool __STD_QUALIFIER 40 operator< __STL_NULL_TMPL_ARGS (const stack&, const stack&); 41 #endif /* __STL_MEMBER_TEMPLATES */ 42 43 public: 44 typedef typename _Sequence::value_type value_type; 45 typedef typename _Sequence::size_type size_type; 46 typedef _Sequence container_type; 47 48 typedef typename _Sequence::reference reference; 49 typedef typename _Sequence::const_reference const_reference; 50 protected: 51 _Sequence c; 52 public: 53 stack() : c() {} 54 explicit stack(const _Sequence& __s) : c(__s) {} 55 56 bool empty() const { return c.empty(); } 57 size_type size() const { return c.size(); } 58 reference top() { return c.back(); } 59 const_reference top() const { return c.back(); } 60 void push(const value_type& __x) { c.push_back(__x); } 61 void pop() { c.pop_back(); } 62 }; 63 64 template <class _Tp, class _Seq> 65 bool operator==(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y) 66 { 67 return __x.c == __y.c; 68 } 69 70 template <class _Tp, class _Seq> 71 bool operator<(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y) 72 { 73 return __x.c < __y.c; 74 } 75 76 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER 77 78 template <class _Tp, class _Seq> 79 bool operator!=(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y) 80 { 81 return !(__x == __y); 82 } 83 84 template <class _Tp, class _Seq> 85 bool operator>(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y) 86 { 87 return __y < __x; 88 } 89 90 template <class _Tp, class _Seq> 91 bool operator<=(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y) 92 { 93 return !(__y < __x); 94 } 95 96 template <class _Tp, class _Seq> 97 bool operator>=(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y) 98 { 99 return !(__x < __y); 100 } 101 102 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */ 103 104 __STL_END_NAMESPACE 105 106 #endif /* __SGI_STL_INTERNAL_STACK_H */ 107 108 // Local Variables: 109 // mode:C++ 110 // End: