概述
stack
是一种先进后出的数据结构,它只有一个出口。
stack
允许新增元素,移除元素,取得最顶端元素。
SGI STL
以deque
作为缺省情况下的stack
底层数据结构。而
stack
是以底部容器完成其所有工作,具有这种“修改某物接口,形成另一种风貌”的性质的,称为配接器,因而,STL stack
往往不被归为容器。
实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
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&); 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() {return c.back();} const_reference top() const {return c.back();} void push(const value_type& x) {c.push_back();} 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; } |
本文为原创文章,版权归Aet所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ Boost 程序库完全开发指南:工具与字符串08/22
- ♥ CLion:配置C++下Nasm开发环境(debian)08/06
- ♥ 51CTO:C++语言高级课程三08/15
- ♥ C++_解码Toml文件08/14
- ♥ 深度探索C++对象模型一02/09
- ♥ C++并发编程 _ 同步并发(Future)05/22