STL系列之二 stack栈
来源:互联网 发布:网络飞行游戏 编辑:程序博客网 时间:2024/06/01 07:13
STL系列之二 stack栈
标签: reference数据结构vectorclassinsertlist
2011-11-09 09:03 48644人阅读 评论(15)收藏举报
本文章已收录于:
分类:
作者同类文章X
- •STL系列十一 随机三趣题——随机重排,文件中随机取一行,生成N个随机数。
- •STL系列之十 全排列(百度迅雷笔试题)
- •STL系列之九 探索hash_set
- •STL系列之八 slist单链表
- •STL系列之七 快速计算x的n次幂 power()的实现
- 更多
版权声明:本文为博主原创文章,未经博主允许不得转载。
栈(statck)这种数据结构在计算机中是相当出名的。栈中的数据是先进后出的(First In Last Out, FILO)。栈只有一个出口,允许新增元素(只能在栈顶上增加)、移出元素(只能移出栈顶元素)、取得栈顶元素等操作。在STL中,栈是以别的容器作为底部结构,再将接口改变,使之符合栈的特性就可以了。因此实现非常的方便。下面就给出栈的函数列表和VS2008中栈的源代码,在STL中栈一共就5个常用操作函数(top()、push()、pop()、 size()、empty() ),很好记的。
VS2008中栈的源代码
友情提示:初次阅读时请注意其实现思想,不要在细节上浪费过多的时间。
- //VS2008中 stack的定义 MoreWindows整理(http://blog.csdn.net/MoreWindows)
- template<class _Ty, class _Container = deque<_Ty> >
- class stack
- { // LIFO queue implemented with a container
- public:
- typedef _Container container_type;
- typedef typename _Container::value_type value_type;
- typedef typename _Container::size_type size_type;
- typedef typename _Container::reference reference;
- typedef typename _Container::const_reference const_reference;
- stack() : c()
- { // construct with empty container
- }
- explicit stack(const _Container& _Cont) : c(_Cont)
- { // construct by copying specified container
- }
- bool empty() const
- { // test if stack is empty
- return (c.empty());
- }
- size_type size() const
- { // test length of stack
- return (c.size());
- }
- reference top()
- { // return last element of mutable stack
- return (c.back());
- }
- const_reference top() const
- { // return last element of nonmutable stack
- return (c.back());
- }
- void push(const value_type& _Val)
- { // insert element at end
- c.push_back(_Val);
- }
- void pop()
- { // erase last element
- c.pop_back();
- }
- const _Container& _Get_container() const
- { // get reference to container
- return (c);
- }
- protected:
- _Container c; // the underlying container
- };
可以看出,由于栈只是进一步封装别的数据结构,并提供自己的接口,所以代码非常简洁,如果不指定容器,默认是用deque来作为其底层数据结构的(对deque不是很了解?可以参阅《STL系列之一 deque双向队列》)。下面给出栈的使用范例:
- //栈 stack支持 empty() size() top() push() pop()
- // by MoreWindows(http://blog.csdn.net/MoreWindows)
- #include <stack>
- #include <vector>
- #include <list>
- #include <cstdio>
- using namespace std;
- int main()
- {
- //可以使用list或vector作为栈的容器,默认是使用deque的。
- stack<int, list<int>> a;
- stack<int, vector<int>> b;
- int i;
- //压入数据
- for (i = 0; i < 10; i++)
- {
- a.push(i);
- b.push(i);
- }
- //栈的大小
- printf("%d %d\n", a.size(), b.size());
- //取栈项数据并将数据弹出栈
- while (!a.empty())
- {
- printf("%d ", a.top());
- a.pop();
- }
- putchar('\n');
- while (!b.empty())
- {
- printf("%d ", b.top());
- b.pop();
- }
- putchar('\n');
- return 0;
- }
转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/6950881
- 顶
- 44
- 踩
- 0
- 上一篇STL系列之一 deque双向队列
- 下一篇STL系列之三 queue 单向队列
相关文章推荐
- •STL系列之二_vector常见函数代码演示
- •c++ STL系列之一 stack与queue
- •关于STLstack栈堆
- •c++ stl栈容器stack的poppush等用法介绍及头文件
- •C++_STL_数据结构_stack_栈
- •STL 模板库中的Stack栈使用
- •STL stack栈
- •STL之stack栈
- •两个栈实现一个队列C++ 利用STL 栈stack实现
- •C++ 与 STL栈stack
阅读全文
0 0
- STL系列之二 stack栈
- STL系列之二 stack栈
- STL系列之二 stack栈
- STL系列之二stack栈
- STL系列之二 stack栈
- STL系列之二 stack栈
- STL系列之二 stack栈
- STL系列之二 stack栈
- STL系列之二 stack栈
- STL系列之二 stack栈
- STL系列之二 stack栈
- STL系列之二 stack栈
- STL系列之二 stack栈
- STL系列之二 stack栈
- STL系列之二 stack栈
- STL系列之二 stack栈
- STL系列之二 stack栈
- STL系列之stack栈
- JAVA大神之路
- 解决Nutz连接Oracle扫描建表,错误信息:无效字符
- [Ubuntu Error]无法获得锁 /var/lib/dpkg/lock
- openGL两种投影方式
- CSS设计的美丽之居中、:before()、:after()的使用(小作品)
- STL系列之二 stack栈
- Ubuntu gcc/g++ 版本切换
- 后门构建工具Backdoor Factory
- jQuery $.ajax() 之登录
- jdk源码解析--深入探讨Iterator模式
- Java中如何遍历Map对象的4种方法
- C++抽象编程——算法分析(8)——插入排序算法与分析
- 单例模式
- mysql存储过程传入表名做参数,进行分页处理