首页 > 代码库 > 标准库Queue的实现
标准库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的实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。