多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
### 前言   stack是一种“先进后出”的数据结构,它只能在栈顶对数据进行操作,即只能在栈顶进行新增元素、移除元素、取得最顶端元素。不能进行遍历行为,所以不需要设计自己的迭代器。在SGI STL的源码<stl_stack.h>的设计中,它是基于某种容器作为底部结构的,默认容器是deque容器,用户也可以自己指定容器的类型。 ### stack容器配接器   由于源码比较短,同时是基于其他容器进行操作的,这里只给出源码的剖析: ~~~ #ifndef __SGI_STL_INTERNAL_STACK_H #define __SGI_STL_INTERNAL_STACK_H #include <sequence_concepts.h> __STL_BEGIN_NAMESPACE // Forward declarations of operators == and <, needed for friend declaration. //这里默认的底层容器类型是deque容器 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); 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 __STL_MEMBER_TEMPLATES 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;//底层容器类型,默认为deque容器 public: //下面对stack的维护完全依赖于底层容器的操作 stack() : c() {} explicit stack(const _Sequence& __s) : c(__s) {} //判断容器是否为空 bool empty() const { return c.empty(); } //获取容器的大小,即容器中元素的个数 size_type size() const { return c.size(); } //返回栈顶元素的引用 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); } #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */ __STL_END_NAMESPACE #endif /* __SGI_STL_INTERNAL_STACK_H */ // Local Variables: // mode:C++ // End: ~~~ 这里给出例子: ~~~ // constructing stacks #include <iostream> // std::cout #include <stack> // std::stack #include <vector> // std::vector #include <deque> // std::deque int main () { std::deque<int> mydeque (3,100); // deque with 3 elements std::vector<int> myvector (2,200); // vector with 2 elements std::stack<int> first; // empty stack std::stack<int> second (mydeque); // stack initialized to copy of deque std::stack<int,std::vector<int> > third; // empty stack using vector std::stack<int,std::vector<int> > fourth (myvector); std::cout << "size of first: " << first.size() << '\n'; std::cout << "size of second: " << second.size() << '\n'; std::cout << "size of third: " << third.size() << '\n'; std::cout << "size of fourth: " << fourth.size() << '\n'; second.push(2); std::cout << "The element at the top of stack second is: " << second.top( ) << "." << std::endl; std::cout << "size of second: " << second.size() << '\n'; return 0; } Output: size of first: 0 size of second: 3 size of third: 0 size of fourth: 2 The element at the top of stack second is:2 . size of second: 4 ~~~ 参考资料: 《STL源码剖析》侯捷