C++在stack的deque实现
本文实现STL在stack大部分功能,同时加入了许多功能。
请注意以下几点:
1.Stack它是一个适配器,在底部vector、list、deque等实现
2.Stack不含有迭代器
在本例中,我加入了几项功能,包含不同类型stack之间的复制和赋值功能,能够实现诸如Stack<int, vector<int> >和Stack<double, list<double> >之间的复制和赋值,这主要依靠成员函数模板来实现。
为了更方便的实现以上功能,我加入了一个函数:
const_container_reference get_container() const
来获取内部容器的引用。
此外,标准库的stack不检查越界行为,我为stack加入了异常处理,当栈空时,运行pop或者top会抛出异常。这个异常类继承自Exception(见上篇文章),用来标示栈空。
具体代码例如以下:Exception的实现见:借助backtrace和demangle实现异常类Exception

#ifndef STACK_HPP_
#define STACK_HPP_ #include "Exception.h"
#include <deque> //栈空引发的异常
class EmptyStackException : public Exception
{
public:
EmptyStackException() :Exception("read empty stack") { }
}; template <typename T, typename Container = std::deque<T> >
class Stack
{
public:
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef Container container_type; //容器类型
typedef EmptyStackException exception_type; //异常类型
typedef typename Container::size_type size_type;
typedef Container &container_reference; //容器引用
typedef const Container& const_container_reference; Stack() { } //不同类型间实现复制
template <typename T2, typename Container2>
Stack<T, Container>(const Stack<T2, Container2> &s); //不同类型间进行赋值
template <typename T2, typename Container2>
Stack<T, Container> &operator=(const Stack<T2, Container2> &s); void push(const value_type &val) { cont_.push_back(val); }
void pop()
{
if(cont_.empty())
throw exception_type();
cont_.pop_back();
} reference top()
{
if(cont_.empty())
throw exception_type();
return cont_.back();
}
const_reference top() const
{
if(cont_.empty())
throw exception_type();
return cont_.back();
} bool empty() const { return cont_.empty(); }
size_type size() const { return cont_.size(); } //获取内部容器的引用
const_container_reference get_container() const
{ return cont_; } friend bool operator==(const Stack &a, const Stack &b)
{
return a.cont_ == b.cont_;
}
friend bool operator!=(const Stack &a, const Stack &b)
{
return a.cont_ != b.cont_;
}
friend bool operator<(const Stack &a, const Stack &b)
{
return a.cont_ < b.cont_;
}
friend bool operator>(const Stack &a, const Stack &b)
{
return a.cont_ > b.cont_;
}
friend bool operator<=(const Stack &a, const Stack &b)
{
return a.cont_ <= b.cont_;
}
friend bool operator>=(const Stack &a, const Stack &b)
{
return a.cont_ >= b.cont_;
} private:
Container cont_;
}; template <typename T, typename Container>
template <typename T2, typename Container2>
Stack<T, Container>::Stack(const Stack<T2, Container2> &s)
:cont_(s.get_container().begin(), s.get_container().end())
{ } template <typename T, typename Container>
template <typename T2, typename Container2>
Stack<T, Container> &Stack<T, Container>::operator=(const Stack<T2, Container2> &s)
{
if((void*)this != (void*)&s)
{
cont_.assign(s.get_container().begin(), s.get_container().end());
} return *this;
} #endif /* STACK_HPP_ */

測试代码例如以下:

#include "Stack.hpp"
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <stdio.h>
using namespace std; int main(int argc, char const *argv[])
{ try
{
Stack<string, vector<string> > st;
st.push("foo");
st.push("bar"); Stack<string, list<string> > st2(st);
//st2 = st; while(!st2.empty())
{
cout << st2.top() << endl;
st2.pop();
} st2.pop(); //引发异常
}
catch (const Exception& ex)
{
fprintf(stderr, "reason: %s\n", ex.what());
fprintf(stderr, "stack trace: %s\n", ex.stackTrace());
} return 0;
}

C++在stack的deque实现的更多相关文章
- deque Comparison of Queue and Deque methods Comparison of Stack and Deque methods
1. 队列queue和双端队列deque的转换 Queue Method Equivalent Deque Methodadd(e) addLast(e)offer(e) offerLast(e)re ...
- java三篇博客转载 详解-vector,stack,queue,deque
博客一:转载自http://shmilyaw-hotmail-com.iteye.com/blog/1825171 java stack的详细实现分析 简介 我们最常用的数据结构之一大概就是stack ...
- STL容器用法速查表:list,vector,stack,queue,deque,priority_queue,set,map
list vector deque stack queue priority_queue set [unordered_set] map [unordered_map] multimap [uno ...
- HDU 5818:Joint Stacks(stack + deque)
http://acm.hdu.edu.cn/showproblem.php?pid=5818 Joint Stacks Problem Description A stack is a data ...
- C++ STL基本容器的使用(vector、list、deque、map、stack、queue)
1.关联容器和顺序容器 C++中有两种类型的容器:顺序容器和关联容器,顺序容器主要有:vector.list.deque等.关联容器主要有map和set.如下图: 1.vector基本使用 #incl ...
- STL容器适配器 stack, queue
stack是一种后进先出(last in first out)的数据结构.它只有一个出口,如图所示.stack允许新增元素,删除元素,取得最顶端元素.但除了最顶端外,没有其他任何地方可以存储stack ...
- 容器适配器之stack
参见http://www.cplusplus.com/reference/stack/stack/ template<class T, class Container = deque<T& ...
- STL之stack
一.stack(栈) 栈:LIFO 后进先出: 首先要指出的是,stack并非和STL的其他类模板是独立的容器,stack是自适应容器(容器适配器) stack<int, deque<in ...
- C++ stack
stack 栈,一种后进先出的数据结构,在c++ stl里作为容器适配器,string,vector,deque,在内存中是连续的 声明方式 stack<int,deque<T>&g ...
随机推荐
- Cordova探险系列(三)
自从3.0之后.Cordova默认是关闭全部关于设备原生特性功能的,所以我们要通过加入插件来启动原生特性. 这里以Accelerometer(加速度感应器)为例,来学习怎样使用设备原生特性. 1.加入 ...
- quartz.net持久化和集群
首先你应该使用的是持久化的quartz,所有定时任务的情况都是保存在数据库表总的,每次启动时,scheduler容器都是按照qrtz_triggers等表内存储的信息来执行定时任务(主要包括cron表 ...
- Apache与Tomcat整合(转)
一 Apache与Tomcat比较联系 apache支持静态页,tomcat支持动态的,比如servlet等. 一般使用apache+tomcat的话,apache只是作为一个转发,对jsp的处理是由 ...
- zoj3829 Known Notation --- 2014 ACM-ICPC Asia Mudanjiang Regional Contest
根据规则,可以发现,一*之前必须有至少2数字.一(11*)这反过来可以被视为一数字. 因此,总位数必须大于*号码,或者你会加入数字. 添加数字后,,为了确保该解决方案将能够获得通过交流.那么肯定是*放 ...
- android AlarmManager采用
Android的闹钟实现机制非常easy, 仅仅须要调用AlarmManager.Set()方法将闹钟设置提交给系统,当闹钟时间到后,系统会依照我们的设定发送指定的广播消息.我们写一个广播去接收消息做 ...
- Directx11学习笔记【十三】 实现一个简单地形
本文由zhangbaochong原创,转载请注明出处http://www.cnblogs.com/zhangbaochong/p/5510294.html 上一个教程我们实现了渲染一个会旋转的立方体, ...
- MyBatis系列教程(六)-- 与Spring综合(Integrate with Spring)
其它工具或技术需要使用: 项目管理工具 : Maven 前台WEB图库:JSP 其他框架:Spring, Spring MVC 数据库 : Derby Maven的Web项目 Maven Depend ...
- dev XtraMessageBox按钮显示中文
dev的XtraMessageBox控件使用起来很美观,但默认显示确定的是英文,如下图: 通过下面代码可使“OK”显示为中文:首先创建一个继承自Localizer的类: using DevExpres ...
- 【C语言探索之旅】 第二课:工欲善其事,必先利其器
内容简介 1.课程大纲 2.第一部分第二课:工欲善其事,必先利其器 3.第一部分第三课预告:你的第一个程序 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C ...
- MySQL InnoDB数据库备份与还原
备份 进入cm黑窗口 输入下列命令 mysqldump -u 用户名 -p 数据库名称> c:\11.sql 回车执行 恢复 进入cm黑窗口 输入下列命令 mysql>use dbtest ...