STL源代码剖析 容器 stl_stack.h
2017-07-01 19:12
274 查看
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
stack
-------------------------------------------------------------stack 是一种配接器(adapter)。以某种容器作为底部结构,改变其接口,使之符合"先进后出"的特性。
SGI STL 默认以 deque 为 stack 底部结构
没有遍历行为,没有遍历器
演示样例:
#include <stack> #include <list> #include <iostream> #include <algorithm> using namespace std; int main(){ stack<int, list<int> >istack; istack.push(1); istack.push(2); cout << istack.size() << endl << istack.top() << endl; }
源代码:
#ifndef __SGI_STL_INTERNAL_STACK_H #define __SGI_STL_INTERNAL_STACK_H __STL_BEGIN_NAMESPACE #ifndef __STL_LIMITED_DEFAULT_TEMPLATES template <class T, class Sequence = deque<T> > //默认以 deque 为底层容器 #else template <class T, class Sequence> #endif class stack { friend bool operator== __STL_NULL_TMPL_ARGS (const stack&, const stack&); friend bool operator< __STL_NULL_TMPL_ARGS (const stack&, const stack&); public: 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: //下面全然利用 Sequence c 的操作完毕 stack 的操作 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(); } //改动接口使符合 stack "前进后出"的特性 void push(const value_type& x) { c.push_back(x); } 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; } __STL_END_NAMESPACE #endif /* __SGI_STL_INTERNAL_STACK_H */ // Local Variables: // mode:C++ // End:
相关文章推荐
- STL源代码剖析 容器 stl_deque.h
- STL源代码剖析 容器 stl_hashtable.h
- STL源代码剖析个人小结3--STL容器
- 利用STL容器stack实现一个简单的计算器
- c++STL容器(map,set,vector,stack,queue)
- STL之容器适配器stack的实现框架
- STL容器-- stack 用法
- 海量数据处理算法 各种STL容器使用的数据结构剖析
- STL源码剖析 [容器](五)[stl_stack.h]
- stl源代码剖析:编译器的提前定义位置集设置
- STL容器 -- Stack
- STL源代码剖析
- STL笔记(3)-deque,queue,stack,list容器
- 【双端队列】【栈】【队列】STL之deque、stack、queue、容器适配器
- C++之STL(十一):容器适配器(stack、 queue 、priority_queue)源码浅析与使用示例
- 【STL】序列式容器--stack和queue
- stl 顺序容器vector(priority_queue),顺序容器List,顺序容器deque(queue, stack)详解
- STL容器之stack
- STL 源代码剖析 算法 stl_algo.h -- inplace_merge
- STL之stack容器