跟上篇实现stack的思路一致,我增加了一些成员函数模板,支持不同类型的Queue之间的复制和赋值。

同时提供一个异常类。

代码如下:

#ifndef QUEUE_HPP
#define QUEUE_HPP #include "Exception.h"
#include <deque> class EmptyQueueException : public Exception
{
public:
EmptyQueueException() :Exception("read empty queue") { }
}; template <typename T, typename Container = std::deque<T> >
class Queue
{
public:
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef Container container_type; //容器类型
typedef EmptyQueueException exception_type; //异常类型
typedef typename Container::size_type size_type;
typedef Container &container_reference; //容器引用
typedef const Container& const_container_reference; explicit Queue(const_container_reference cont = container_type()) :cont_(cont) { } template <typename T2, typename Container2>
Queue<T, Container>(const Queue<T2, Container2> &other); template <typename T2, typename Container2>
Queue<T, Container> &operator=(const Queue<T2, Container2> &other); void push(const value_type &val)
{
cont_.push_back(val);
} void pop()
{
if(cont_.empty())
throw exception_type();
cont_.pop_front();
} reference front()
{
if(cont_.empty())
throw exception_type();
return cont_.front();
}
const_reference front() const
{
if(cont_.empty())
throw exception_type();
return cont_.front();
}
reference back()
{
if(cont_.empty())
throw exception_type();
return cont_.back();
}
const_reference back() 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 Queue &a, const Queue &b)
{
return a.cont_ == b.cont_;
}
friend bool operator!=(const Queue &a, const Queue &b)
{
return a.cont_ != b.cont_;
}
friend bool operator<(const Queue &a, const Queue &b)
{
return a.cont_ < b.cont_;
}
friend bool operator>(const Queue &a, const Queue &b)
{
return a.cont_ > b.cont_;
}
friend bool operator<=(const Queue &a, const Queue &b)
{
return a.cont_ <= b.cont_;
}
friend bool operator>=(const Queue &a, const Queue &b)
{
return a.cont_ >= b.cont_;
} private:
container_type cont_;
}; template <typename T, typename Container>
template <typename T2, typename Container2>
Queue<T, Container>::Queue(const Queue<T2, Container2> &other)
:cont_(other.get_container().begin(), other.get_container().end())
{ } template <typename T, typename Container>
template <typename T2, typename Container2>
Queue<T, Container> &Queue<T, Container>::operator=(const Queue<T2, Container2> &other)
{
cont_.assign(other.get_container().begin(), other.get_container().end());
} #endif //QUEUE_HPP

测试代码如下:

#include "Stack.hpp"
#include "Queue.hpp"
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <stdio.h>
using namespace std; int main(int argc, char const *argv[])
{ try
{
Queue<string, vector<string> > st;
st.push("foo");
st.push("bar"); Queue<string, list<string> > st2(st);
//st2 = st; while(!st2.empty())
{
cout << st2.front() << 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;
}

标准库Queue的实现的更多相关文章

  1. python MultiProcessing标准库使用Queue通信的注意要点

    今天原本想研究下MultiProcessing标准库下的进程间通信,根据 MultiProcessing官网 给的提示,有两种方法能够来实现进程间的通信,分别是pipe和queue.因为看queue顺 ...

  2. C++标准库类模板(stack)和 队列(queue)

    在C++标准库(STL)中有栈和队列的类模板,因此可以直接使用 1.栈(stack):使用栈之前,要先包含头文件 : #include<stack> stack.push(elem); / ...

  3. C++标准库分析总结(五)——<Deque、Queue、Stack设计原则>

    本节主要总结标准库Deque的设计方法和特性以及相关迭代器内部特征 1.Deque基本结构 Deque(双向队列)也号称连续空间(其实是给使用者一个善意的谎言,只是为了好用),其实它使用分段拼接起来的 ...

  4. PHP SPL(PHP 标准库)

    一.什么是SPL? SPL是用于解决典型问题(standard problems)的一组接口与类的集合.(出自:http://php.net/manual/zh/intro.spl.php) SPL, ...

  5. PHP标准库 (SPL) 笔记

    简介 SPL是Standard PHP Library(PHP标准库)的缩写. The Standard PHP Library (SPL) is a collection of interfaces ...

  6. 【转】C++标准库和标准模板库

    C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标准头文件中定义.在C++开发中,要尽可能地利用标准库完成.这样做的直接好处包括:(1)成本:已经作为标准提供,何苦再花费 ...

  7. python标准库00 学习准备

    Python标准库----走马观花 python有一套很有用的标准库.标准库会随着python解释器一起安装在你的电脑上的.它是python的一个组成部分.这些标准库是python为你准备的利器,可以 ...

  8. C++标准库简介、与STL的关系。

    转自http://www.cnblogs.com/xiongjiaji/archive/2011/06/22/2476490.html C++标准库的所有头文件都没有扩展名.C++标准库的内容总共在5 ...

  9. 【循序渐进学Python】11.常用标准库

    安装完Python之后,我们也同时获得了强大的Python标准库,通过使用这些标准库可以为我们节省大量的时间.这里是一些常用标准库的简单说明.更多的标准库的说明,可以参考Python文档 sys 模块 ...

随机推荐

  1. WPF - 样式 (转)

    本文目录 1.引言 2.怎样使用样式? 3.内联样式 4.已命名样式 5.元素类型样式 6.编程控制样式 7.触发器 1.引言 样式(Style),主要是用来让元素或内容呈现一定外观的属性.WPF中的 ...

  2. BZOJ 4261: 建设游乐场

    4261: 建设游乐场 Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 38  Solved: 16[Submit][Status][Discuss] ...

  3. 孙鑫 VC++深入详解第14课——TCP通信/UDP通信(转)

    原文转自 http://blog.csdn.net/supersonico/article/details/18900319 一.用VC++来编写TCP 服务器/客户端的简单程序. 注意事项: 1.要 ...

  4. VIM使用系列: 复制并移动文本

    1 5. 复制并移动文本 *copy-move* 2 3 *quote* 4 "{a-zA-Z0-9.%#:-"} 指定下次的删除.抽出和放置命令使用的寄存器 5 {a-zA-Z0 ...

  5. 动态加载.so文件并执行类函数

    背景:不同产品组将其功能编译为.so,这些.so 可以加载到统一的基础平台上运行,如果产品组代码有改动,只需要更新对应的.so 问题:如何动态加载.so文件,并使用里边的函数/类 ? 解决方法1: 使 ...

  6. 为何url地址不是直接发送到服务器,而是被编码后再发送

    首先,先说一下,关于为何必须将url地址,去编码后,再发送,是因为相关的协议规范:RFC 1738,定义了url地址中不能包含除了0-9的数字,大小写字母(a-zA-Z),短横线’-‘ 之外的字母.换 ...

  7. C#实时读取数据----局部页面刷新【转】

    I)现在刚开始学习C#,对一些基本的控件了解的不够,有个实时监控的系统,需要页面中的数据每5秒钟刷新一次, 要是每5秒钟页面全部的刷新,那页面根本就没法看了,对这个问题在CSDN上也专门开了帖子,问了 ...

  8. 让Asp.net Web预启动

    IIS8以下解决方案: 当我们把网站部署在IIS7或IIS6S的时候,每当IIS或是Application Pool重启后,第一次请求网站反应总是很慢,原因大家都知道(不知道可以参考这个动画说明ASP ...

  9. Python_Tips[3] -> sort/sorted 排序函数

    排序函数 / Sort Function list自带的sort函数可以实现对列表的排列功能,具有同样功能的还有sorted函数. 基本形式 列表有自己的sort方法,其对列表进行原址排序,既然是原址 ...

  10. SpringBoot整合Zookeeper和Dubbo

    一.Dubbo 1. Dubbo定义 Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合).从服务模型的角度来 ...