首页 技术 正文
技术 2022年11月20日
0 收藏 522 点赞 3,620 浏览 1931 个字

从零开始写STL—栈和队列

适配器模式

  • 意图:将一个类的接口转换成客户希望的另外一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
  • 主要解决:主要解决在软件系统中,常常要将一些"现存的对象"放到新的环境中,而新环境要求的接口是现对象不能满足的。
  • 何时使用: 1、系统需要使用现有的类,而此类的接口不符合系统的需要。 2、想要建立一个可以重复使用的类,用于与一些彼此之间没有太大关联的一些类,包括一些可能在将来引进的类一起工作,这些源类不一定有一致的接口。 3、通过接口转换,将一个类插入另一个类系中。(比如老虎和飞禽,现在多了一个飞虎,在不增加实体的需求下,增加一个适配器,在里面包容一个虎对象,实现飞的接口。)
  • 如何解决:继承或依赖(推荐)。

基于deque的栈

template<class T,class Sequence = deque<T>>
class stack
{
public:
typedef T value_type;
typedef T& reference;
typedef size_t size_type;
protected:
Sequence c;
public:
stack() :c() {}
bool empty()
{
return c.empty();
}
size_type size()
{
return c.size();
}
value_type& top()
{
if(!empty())
return *(c.begin());
else
{
std::cerr << "Top on empty Stack!" << std::endl;
std::exit(1);
}
}
void pop()
{
if(!empty())
c.pop_front();
else
{
std::cerr << "pop on empty Stack!" << std::endl;
std::exit(1);
}
}
void push(const value_type& x)
{
c.push_front(x);
}
stack<T> swap(stack<T>& rhs)
{
c.swap(rhs.c);
return *this;
}
bool operator==(stack<T> &rhs)
{
return c == rhs.c;
}
bool operator!=(stack<T>& rhs)
{
return c != rhs.c;
}
};
template<typename T>
void swap(stack<T>& a, stack<T>& b)
{
a.swap(b);
}

基于deque的队列

template<class T,class Sequence = deque<T>>
class queue
{
public:
typedef T value_type;
typedef T& reference;
typedef size_t size_type;
typedef typename Sequence::iterator iterator;
protected:
Sequence c;
void check()
{
if (empty())
{
std::cerr << "Empty !" << std::endl;
std::exit(1);
}
}
public:
queue():c(){}iterator begin()
{
return c.begin();
}iterator end()
{
return c.end();
}bool empty()
{
return c.empty();
}size_type size()
{
return c.size();
}
value_type& front()
{
check();
return *(c.begin());
}
value_type& back()
{
check();
auto tmp = c.end();
tmp--;
return *(tmp);
}
const value_type& front() const
{
check();
return *(c.begin());
}
const value_type& back() const
{
check();
return *(c.end() - 1);
}
void push(const value_type& x)
{
c.push_back(x);
}
void pop()
{
check();
c.pop_front();
}
bool operator==( queue<T>& rhs)
{
return c == rhs.c;
}
bool operator!=( queue<T>& rhs)
{
return !(*this == rhs);
}
queue<T>& swap(queue<T>& rhs)
{
c.swap(rhs.c);
return *this;
}};
template<typename T>
void swap(queue<T> &a, queue<T> &b)
{
a.swap(b);
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,405
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898