标准库Queue的实现
跟上篇实现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的实现的更多相关文章
- python MultiProcessing标准库使用Queue通信的注意要点
今天原本想研究下MultiProcessing标准库下的进程间通信,根据 MultiProcessing官网 给的提示,有两种方法能够来实现进程间的通信,分别是pipe和queue.因为看queue顺 ...
- C++标准库类模板(stack)和 队列(queue)
在C++标准库(STL)中有栈和队列的类模板,因此可以直接使用 1.栈(stack):使用栈之前,要先包含头文件 : #include<stack> stack.push(elem); / ...
- C++标准库分析总结(五)——<Deque、Queue、Stack设计原则>
本节主要总结标准库Deque的设计方法和特性以及相关迭代器内部特征 1.Deque基本结构 Deque(双向队列)也号称连续空间(其实是给使用者一个善意的谎言,只是为了好用),其实它使用分段拼接起来的 ...
- PHP SPL(PHP 标准库)
一.什么是SPL? SPL是用于解决典型问题(standard problems)的一组接口与类的集合.(出自:http://php.net/manual/zh/intro.spl.php) SPL, ...
- PHP标准库 (SPL) 笔记
简介 SPL是Standard PHP Library(PHP标准库)的缩写. The Standard PHP Library (SPL) is a collection of interfaces ...
- 【转】C++标准库和标准模板库
C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标准头文件中定义.在C++开发中,要尽可能地利用标准库完成.这样做的直接好处包括:(1)成本:已经作为标准提供,何苦再花费 ...
- python标准库00 学习准备
Python标准库----走马观花 python有一套很有用的标准库.标准库会随着python解释器一起安装在你的电脑上的.它是python的一个组成部分.这些标准库是python为你准备的利器,可以 ...
- C++标准库简介、与STL的关系。
转自http://www.cnblogs.com/xiongjiaji/archive/2011/06/22/2476490.html C++标准库的所有头文件都没有扩展名.C++标准库的内容总共在5 ...
- 【循序渐进学Python】11.常用标准库
安装完Python之后,我们也同时获得了强大的Python标准库,通过使用这些标准库可以为我们节省大量的时间.这里是一些常用标准库的简单说明.更多的标准库的说明,可以参考Python文档 sys 模块 ...
随机推荐
- 刨根问底Objective-C Runtime(4)- 成员变量与属性
http://chun.tips/blog/2014/11/08/bao-gen-wen-di-objective[nil]c-runtime(4)[nil]-cheng-yuan-bian-lian ...
- C++ 播放音频流(PCM裸流)--改善
直接上代码,如果有需要可以直接建一个win32控制台程序然后将代码拷过去改个文件名就可以用了(注意将声道和频率与你自己的文件对应).当然我自己也用VS2008写了个例子上传了,如果有需要下载地址如下: ...
- Selenium2+python自动化3-解决pip使用异常【转载】
一.pip出现异常 有一小部分童鞋在打开cmd输入pip后出现下面情况:Did not provide a commandDid not provide a command?这是什么鬼?正常情况应该是 ...
- AC日记——[WC2013]糖果公园 cogs 1817
[WC2013]糖果公园 思路: 带修改树上莫队(模板): 来,上代码: #include <cmath> #include <cstdio> #include <cst ...
- JavaScript基础之原型对象和原型链
原型对象 原型对象简单来说就是函数的原型所指向的对象.前面说原型的时候,说了Object.prototype所指对象就是Object(函数)的原型对象.在每个函数的原型对象中,默认会有construc ...
- Spring:特殊数据类型的属性注入(基于配置文件)
该处提到的特殊数据类型指的是除了基础数据类型和String以外的其他常用的数据类型,如:List.Map.Set.以及pojo对象等.则我们创建的Person类定义为: package bjtu.we ...
- Educational Codeforces Round 33 (Rated for Div. 2) B. Beautiful Divisors【进制思维/打表】
B. Beautiful Divisors time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- kibana-Coordinate Map
1. Visualize 添加图形 2. 选择图形类型 Coordinate Map 3. 选择索引 4. 设定成图的聚合字段 如果有数据,点击右上角的三角形,地图上就会有显示. 5. 保存图形
- jquery 为表单动态添加元素
$('<input />').attr('type','hidden') .attr('name','type') .attr('value', ...
- luogu P1529 回家 Bessie Come Home
题目描述 现在是晚餐时间,而母牛们在外面分散的牧场中. 农民约翰按响了电铃,所以她们开始向谷仓走去. 你的工作是要指出哪只母牛会最先到达谷仓(在给出的测试数据中,总会有且只有一只最快的母牛). 在挤奶 ...