标准库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 模块 ...
随机推荐
- hdu 6119 …&&百度之星 T6
小小粉丝度度熊 Problem Description 度度熊喜欢着喵哈哈村的大明星——星星小姐. 为什么度度熊会喜欢星星小姐呢? 首先星星小姐笑起来非常动人,其次星星小姐唱歌也非常好听. 但这都不是 ...
- Java中的抽象和封装
一.面向对象和面向过程的区别 面向对象: 核心:封装了属性和方法的类,以数据为中心,实现了类级别的代码重用 面向对象因为采用了类,所以具有继承和多态特性,可以进一步重用代码和简化编程 面向过程: 核心 ...
- Asp .Net MVC中常用过滤属性类
/// <summary> /// /// </summary> public class AjaxOnlyAttribute : ActionFilterAttribute ...
- [bzoj1026][SCOI2009]windy数——数位dp
题目 求[a,b]中的windy数个数. windy数指的是任意相邻两个数位上的数至少相差2的数,比如135是,134不是. 题解 感觉这个题比刚才做的那个简单多了...这个才真的应该是数位dp入门题 ...
- 【问题帖】压缩图片大小至指定Kb以下
像PS,QQ影像等都有该功能,将图片大小压缩至指定kb以下. 我也来山寨一把,到目前为止,控制图片的大小,平时的解决方案通过分辨率和质量来控制的. 假定最后压缩的大小是100kb,那么在保证不大于10 ...
- xen hypercall 的应用层实现
一句话描述: xen hypercall 在应用层的实现,最终都变成对 /proc/xen/privcmd 的 ioctl 系统调用 我们知道,xen 在应用层最上层的接口是 libxl , 基本上 ...
- python3使用urllib获取set-cookies
#!/usr/bin/env python # encoding: utf-8 import urllib.request from collections import defaultdict re ...
- selenium的动作链 actionchains
click(on_element=None) ——单击鼠标左键 click_and_hold(on_element=None) ——点击鼠标左键,不松开 context_click(on_elemen ...
- Webpack & The Hot Module Replacement热模块替换原理解析
Webpack & The Hot Module Replacement热模块替换原理解析 The Hot Module Replacement(HMR)俗称热模块替换.主要用来当代码产生变化 ...
- HTTP GET与POST区别
HTTP定义了与服务器交互的不同方法,最基本的方法是 GET 和 POST. HTTP-GET和HTTP-POST是使用HTTP的标准协议动词,用于编码和传送变量名/变量值对参数,并且使用相关的请求语 ...