stack in c++
来源:互联网 发布:淘宝网-淘我喜欢首页 编辑:程序博客网 时间:2024/05/01 06:50
stack在c++stl中作为特殊容器存在,其实现多为单纯地把各项操作转化为内部容器的对应调用
下面是其简单实现
头文件
#ifndef _STACK_H#define _STACK_H#include<deque>#include<exception>template <typename T>class Stack{ protected: std::deque<T> c; public: class ReadEmptyStack:public std::exception{ public: virtual const char* what() const throw(){ return "read empty stack"; } };typename std::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_c++.h"#include<iostream>using namespace std;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;}}
阅读全文
0 0
- Stack & Heap in C/C++
- Stack & Heap in C/C++ .
- Stack & Heap in C/C++
- stack changes when calling function in c
- Stack ---- Implementation in C and Python
- Stack and Heap Objects in Objective-C
- Create a table for LUA stack in C
- Obtaining a stack trace in C upon SIGSEGV
- Use linked list to create queue stack BST in C
- Memory management in C: The heap and the stack
- c++:stack
- Stack Implementation in C++
- Stack Walking in Xperf
- Stack & Heap in Java
- stack in c++
- [iOS开发站在巨人肩膀上]之How to implement a stack in Objective-C
- Friday Q&A 2010-01-15: Stack and Heap Objects in Objective-C
- Structure of a C-Program in Memory | How Heap,Stack,Data and Code segments are stored in memory?
- 控制easyui-tabs打开哪个标签页、选项卡,及easyui-datagrid单击事件
- Android 异步消息处理机制 让你深入理解 Looper、Handler、Message三者关系
- bzoj3810[Coci2015]Stanovi 记忆化搜索
- 单一职责原则(SRP)
- 科技界远比商业界更缺乏温情,更弱肉强食
- stack in c++
- IDEA配置JDK
- ORB算法描述
- Oracle中的Round函数
- shell中的特殊变量IFS(转)
- volatile 关键字
- Linux用户管理
- 2----数据的艺术
- sql语句优化