跟上篇实现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. 废弃sqlite代码,备查

    using System.Linq; using System.Text; using System.Threading.Tasks; using System.Reflection; using T ...

  2. python delete数据

    #!/usr/bin/env python # -*- coding:utf-8 -*- # @Time : 2017/11/24 0:27 # @Author : lijunjiang # @Fil ...

  3. hdu 2489(状态压缩+最小生成树)

    Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. 华农oj Problem B: Averyboy找密码【STL】

    Problem B: Averyboy找密码 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 83 Solved: 29 [Submit][Status] ...

  5. Mybatis批量添加,删除与修改

    1.批量添加元素session.insert(String string,object O) public void batchInsertStudent(){ List<Student> ...

  6. 用fiddler测试移动端翻页

    大家在移动端是怎么测试翻页的,肯定都是下拉或上滑吧,我也是这样测试的 但如果你要验证数据是否与pc端数据一致时,可能是第一页,第二页看看,或最后几页数据看看,在pc端看简单,直接点击最后一页就行,在移 ...

  7. [JOISC2016]サンドイッチ

    题目大意: 一个$n\times m(n,m\leq400)$的网格图中,每个格子上放了两个三明治,摆放的方式分为'N'和'Z'两种.一个三明治可以被拿走当且仅当与该三明治的两条直角边相邻的三明治均被 ...

  8. 在java代码中设置margin

    我们平常可以直接在xml里设置margin,如: <ImageView android:layout_margin="5dip" android:src="@dra ...

  9. Ubuntu 16.04下ssh启用root登录

    debian里root账户默认没有密码,但账户锁定. 当需要root权限时, 直接执行 sudo su 即可切换为root用户. 重新开启root账号,在命令行下执行 sudo passwd root ...

  10. 开始使用 Docker (Linux 上运行 SQL Server) 上的 SQL Server 容器 - SQL Server | Microsoft Docs

    原文:开始使用 Docker (Linux 上运行 SQL Server) 上的 SQL Server 容器 - SQL Server | Microsoft Docs 快速入门:使用 Docker ...