跟上篇实现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. 自己写的enum转换的一个扩展,

    public static String ToEnumName(this int? source, Type e) { if (!source.HasValue) throw new Argument ...

  2. mysql 事务、游标

    mysql 事务 在MySQL中只有使用了Innodb数据库引擎的数据库或表才支持事务,所以很多情况下我们都使用innodb引擎. 事务处理可以用来维护数据库的完整性,保证成批的SQL语句要么全部执行 ...

  3. linux文件名匹配

    *   匹配文件名中的任何字符串,包括空字符串. ? 匹配文件名中的任何单个字符. [...]   匹配[ ]中所包含的任何字符. [!...]   匹配[ ]中非感叹号!之后的字符. 如: s*   ...

  4. Codeforces Beta Round #4 (Div. 2 Only) C. Registration system【裸hash/map】

    C. Registration system time limit per test 5 seconds memory limit per test 64 megabytes input standa ...

  5. HDU 5886 Tower Defence(2016青岛网络赛 I题,树的直径 + DP)

    题目链接  2016 Qingdao Online Problem I 题意  在一棵给定的树上删掉一条边,求剩下两棵树的树的直径中较长那的那个长度的期望,答案乘上$n-1$后输出. 先把原来那棵树的 ...

  6. bzoj3225 [Sdoi2008]立方体覆盖——扫描线

    3225: [Sdoi2008]立方体覆盖 Description A君近日为准备省队选拔,特意进行了数据结构的专项训练.训练过程中就遇到了“矩形面积并”这道经典问题,即:给出N个各边与坐标轴平行(垂 ...

  7. HDU 4034 Graph Floyd最短路

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题意: 给你一个最短路的表,让你还原整个图,并使得边最少 题解: 这样想..这个表示通过floy ...

  8. 【bzoj4808】【马】二分图最大点独立集+简单感性证明

    (上不了p站我要死了,侵权度娘背锅) Description 众所周知,马后炮是中国象棋中很厉害的一招必杀技."马走日字".本来,如果在要去的方向有别的棋子挡住(俗称"蹩 ...

  9. JavaSript模块规范 - AMD规范与CMD规范介绍 (转)

    JavaSript模块化   在了解AMD,CMD规范前,还是需要先来简单地了解下什么是模块化,模块化开发?       模块化是指在解决某一个复杂问题或者一系列的杂糅问题时,依照一种分类的思维把问题 ...

  10. Linux文件名小写的好处(转)

    说明:来自老阮的<为什么文件名要小写>的文章,其实我觉得应该说是<Linux文件名为什么要小写>会更合适些. 一.可移植性 Linux 系统是大小写敏感的,而 Windows ...