首页 > 代码库 > Queue的简单实现
Queue的简单实现
代码如下:
1 #ifndef QUEUE_H 2 #define QUEUE_H 3 #include "Exception.h" 4 #include <deque> 5 6 class EmptyQueueException : public Exception 7 { 8 public: 9 EmptyQueueException() 10 :Exception("read empty queue") 11 { } 12 }; 13 14 template <typename T, typename Container = std::deque<T> > 15 class Queue 16 { 17 public: 18 19 typedef T value_type; 20 typedef T& reference; 21 typedef const T& const_reference; 22 typedef Container container_type; 23 typedef Container& container_reference; 24 typedef const Container& const_container_reference; 25 typedef EmptyQueueException exception_type; 26 typedef typename Container::size_type size_type; 27 28 29 explicit Queue(const_container_reference cont = container_type()) 30 :_cont(cont) 31 { } 32 33 template <typename T2, typename Container2> 34 Queue<T, Container>(const Queue<T2, Container2> &s); 35 36 template <typename T2, typename Container2> 37 Queue<T, Container> &operator=(const Queue<T2, Container2> &s); 38 39 void push(const value_type &val) { _cont.push_back(val); } 40 41 void pop() 42 { 43 if(_cont.empty()) 44 throw exception_type(); 45 _cont.pop_front(); 46 } 47 48 reference front() 49 { 50 if(_cont.empty()) 51 throw exception_type(); 52 return _cont.front(); 53 } 54 55 const_reference front() const 56 { 57 if(_cont.empty()) 58 throw exception_type(); 59 return _cont.front(); 60 } 61 62 reference back() 63 { 64 if(_cont.empty()) 65 throw exception_type(); 66 return _cont.back(); 67 } 68 69 const_reference back() const 70 { 71 if(_cont.empty()) 72 throw exception_type(); 73 return _cont.back(); 74 } 75 76 bool empty() const { return _cont.empty(); } 77 size_type size() const { return _cont.size(); } 78 79 const_container_reference get_container() const 80 { return _cont; } 81 82 friend bool operator== (const Queue &a, const Queue &b) 83 { return a._cont == b._cont; } 84 85 friend bool operator!= (const Queue &a, const Queue &b) 86 { return a._cont != b._cont; } 87 88 friend bool operator>= (const Queue &a, const Queue &b) 89 { return a._cont >= b._cont; } 90 91 friend bool operator<= (const Queue &a, const Queue &b) 92 { return a._cont <= b._cont; } 93 94 friend bool operator> (const Queue &a, const Queue &b) 95 { return a._cont > b._cont; } 96 97 friend bool operator< (const Queue &a, const Queue &b) 98 { return a._cont < b._cont; } 99 private:100 container_type _cont;101 };102 103 template <typename T, typename Container>104 template <typename T2, typename Container2>105 Queue<T, Container>::Queue(const Queue<T2, Container2> &s)106 :_cont(s.get_container().begin(), s.get_container().end())107 { }108 109 template <typename T, typename Container>110 template <typename T2, typename Container2>111 Queue<T, Container> &Queue<T, Container>::operator= (const Queue<T2, Container2> &s)112 {113 if((void *)this != (void *)&s)114 {115 _cont.assign(s.get_container().begin(), s.get_container().end());116 }117 return *this;118 }119 #endif
测试代码如下:
1 #include "stack.hpp" 2 #include "queue.hpp" 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <list> 7 #include <stdio.h> 8 using namespace std; 9 10 int main(int argc, char const *argv[])11 {12 13 try14 {15 Queue<string, vector<string> > st;16 st.push("foo");17 st.push("bar");18 19 Queue<string, list<string> > st2(st);20 //st2 = st;21 22 while(!st2.empty())23 {24 cout << st2.front() << endl;25 st2.pop();26 }27 28 st2.pop(); //引发异常29 }30 catch (const Exception& ex)31 {32 fprintf(stderr, "reason: %s\n", ex.what());33 fprintf(stderr, "stack trace: %s\n", ex.stackTrace());34 }35 36 return 0;37 }
测试结果如下:
foobarreason: read empty queuestack trace: ./a.out()./a.out()./a.out()./a.out()./a.out()/lib/libc.so.6(__libc_start_main+0xe6)./a.out()
Queue的简单实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。