标准库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;
}
时间: 2024-08-26 18:04:11

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

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

今天原本想研究下MultiProcessing标准库下的进程间通信,根据 MultiProcessing官网 给的提示,有两种方法能够来实现进程间的通信,分别是pipe和queue.因为看queue顺眼,就想着拿queue实现,后来,被坑了....于是有了这篇文章.我按照 python标准库之MultiProcessing库的研究 (1) 里面的代码来的,结果就是不断的出错,死过就是不出结果,看看程序: from multiprocessing import Pool, queues impor

C++标准库和标准模板库

C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标准头文件中定义. 在C++开发中,要尽可能地利用标准库完成.这样做的直接好处包括: (1)成本:已经作为标准提供,何苦再花费时间.人力重新开发呢: (2)质量:标准库的都是经过严格测试的,正确性有保证: (3)效率:关于人的效率已经体现在成本中了,关于代码的执行效率要相信实现标准库的大牛们的水平: (4)良好的编程风格:采用行业中普遍的做法进行开发. 一.C++标准库 C++标准库的内容分为10类, 分别是:C1.语

C++标准库——顺序容器

容器是容纳特定类型对象的集合.顺序容器将单一类型元素聚集起来,并且根据位置来存储和访问这些元素.顺序容器中元素排列顺序与元素值无关,而是根据元素值添加到容器中的次序决定的. 标准库中有三种顺序容器,分别是vector,list与deque.其中vector支持随机的快速访问,因为vector中存放数据是在内存中连续存放的.在实际的实现中,vector会提前申请一块较大的内存空间(如果指定了大小,一般是指定大小的两倍),这样每次加入新的元素时不用再申请新的内存空间,提高效率.list容器是类似于链

第32课 - 初探C++ 标准库

第32课 - 初探C++ 标准库 1. 有趣的重载 操作符 << 的原生意义是按位左移,例: 1  <<  2 ; 其意义是将整数 1 按位左移 2 位,即: 0000 0001   ->    0000 0100 重载左移操作符,将变量或常量左移到一个对象中! 1 #include <stdio.h> 2 3 const char endl = '\n'; 4 5 class Console 6 { 7 public: 8 Console& operat

Python标准库——走马观花

Python标准库——走马观花 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python有一套很有用的标准库(standard library).标准库会随着Python解释器,一起安装在你的电脑中的.它是Python的一个组成部分.这些标准库是Python为你准备好的利器,可以让编程事半功倍. 我将根据我个人的使用经验中,挑选出标准库三个方面的包(package)介绍: Python增强 系统互动 网络 第一类:Pyth

C++ 标准库概览(一分钟就看完了)

C++ 标准库以若干头文件的方式提供. 下面简单介绍一个各头文件的内容. 第一部分 容器 Containers <array> C++11 新增.提供了容器类模板 std::array,固定大小数组的容器. <bitset> 提供了专门用来存放位组(一系列 bit)的容器类 std::bitset. <deque> 提供了双向队列容器类模板 std::deque. <forward_list> C++11 新增.提供了单向链表容器类模板 std::forwa

《C++ Primer》第II部分:C++标准库

<C++ Primer>第II部分:C++标准库 前言 把<C++ Primer>读薄系列笔记.本篇为第II部分C++标准库,包含全书第8-12章重难点: IO库 顺序容器 范型算法 关联容器 动态内存 修订版课后题解见GitHub仓库cpp-primer-workbook. IO库 IO类继承机制:ifstream和istringstream继承自istream,ofstream和ostringstream都继承自ostream 宽字符IO类:在函数和类型前加前缀w,如wcin.

《Python基础教程(第二版)》学习笔记 -&gt; 第十章 充电时刻 之 标准库

SYS sys这个模块让你能够访问与Python解释器联系紧密的变量和函数,下面是一些sys模块中重要的函数和变量: 函数和变量 描述 argv 命令行参数,包括脚本和名称 exit([arg])                退出当前的程序,可选参数为给定的返回值或者错误信息 modules 映射模块名字到载入模块的字典 path 查找模块所在目录的目录名列表 platform 平台标识符 stdin 标准输入流-- 一个类文件对象 stdout 标准输出流-- 一个类文件对象 stderr

c++标准库和stl

C++中有三大重要的标准库,为string.vector.bitset,他们每个都是一个类,对应的命名空间均为std.string类的对象可以存 储一个字符串,相应于C中存储字符串的方式,C++的优点是,在创建对象时可以不指定长度,在连接和删除中,只需要使用进行算符重载后了的“+”和 “-”.vector类的对象可以存储一个数组,可以int,char,string等,使用时,就像一个栈一样,通过push_back. pop_back等进行操作,这是与一般数组不一样的地方. String 类 1