stack 改进版
来源:互联网 发布:域名管理 新网 编辑:程序博客网 时间:2024/06/09 23:28
<pre name="code" class="cpp">/* C++ 标准库作者修改版的stack优点1、pop()会返回下一元素2、 如果stack为空,pop()和top()会抛出异常
*/#ifndef STACK_HP#define STACK_HPP#include<deque>#include<exception>using namespace std;template<class T>class Stack{protected:deque<T> c; //container for the elementspublic:class ReadEmptyStack :public exception{public:virtual const char* what() const throw(){return "Read empty stack!";}};typename deque<T>::size_type size() const{return c.size();}bool empty() const {return c.empty();}void push(const T& elem){c.push_back(elem);}T pop(){if (c.empty()){ throw ReadEmptyStack(); }T elem(c.back());c.pop_back();return elem;}T &top(){if (c.empty()){throw ReadEmptyStack();}return c.back();}};#endif
#include"Stack.h"#include<iostream>int main(){try{Stack<int>st;st.push(1);st.push(2);st.push(3);cout << st.pop() << " ";cout << st.pop() << " ";st.top() = 77;st.push(4);st.push(5);st.pop();cout << st.pop() << " ";cout << st.pop() << endl;cout << st.pop() << endl; }catch (const exception &e){cerr << "Exception: " << e.what() << endl;}system("pause");return 0;}
0 0
- stack 改进版
- Java Collections Framework之Stack源码分析缺陷,栈改进版(通过LinkedList实现)(基于JDK1.6)
- 改进版Community预览
- 改进版Community说明
- FreeTextBox改进版
- FreeTextBox改进版
- 改进版CTEXT
- 双人五子棋改进版
- 银行系统改进版
- 汉诺塔改进版
- 成绩处理改进版
- 银行系统。。改进版
- Python俄罗斯方块改进版
- ADO.NET改进版
- KMP算法(改进版)
- 冒泡排序改进版
- 折腾数组改进版
- AFNetworkingV2Client改进版
- Java基础(16):多线程
- flask小结
- ecshop导航栏自动显示三级或多级子栏目,多级频道分类,并实现css高亮显示
- linux命令复用技巧
- Feature extraction, foundation and application笔记
- stack 改进版
- 常见排序及搜索算法
- CVPR2015 interesting paper(Part 1)
- Android第三方FloatingActionButton:伴随ListView、RecyclerView、ScrollView滚动滑入滑出
- 三、位操作(来自wiki)
- [黑马IOS自学第九篇]OC语言基础,面向对象概念,类的创建,OC多文件开发等
- 基础练习 龟兔赛跑预测
- awk之变量篇
- graylog2 实践