博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Queue的简单实现
阅读量:6944 次
发布时间:2019-06-27

本文共 3794 字,大约阅读时间需要 12 分钟。

代码如下:

1 #ifndef QUEUE_H  2 #define QUEUE_H  3 #include "Exception.h"  4 #include 
5 6 class EmptyQueueException : public Exception 7 { 8 public: 9 EmptyQueueException() 10 :Exception("read empty queue") 11 { } 12 }; 13 14 template
> 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
34 Queue
(const Queue
&s); 35 36 template
37 Queue
&operator=(const Queue
&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
104 template
105 Queue
::Queue(const Queue
&s)106 :_cont(s.get_container().begin(), s.get_container().end())107 { }108 109 template
110 template
111 Queue
&Queue
::operator= (const Queue
&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 
4 #include
5 #include
6 #include
7 #include
8 using namespace std; 9 10 int main(int argc, char const *argv[])11 {12 13 try14 {15 Queue
> st;16 st.push("foo");17 st.push("bar");18 19 Queue
> 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()

 

转载于:https://www.cnblogs.com/gjn135120/p/4007340.html

你可能感兴趣的文章
centos/ubuntu挂载vmdk、 vdi为块设备的方法(非vdfuse)
查看>>
Exchange server 2003迁移到2010后,手动更新地址列表,提示OAB Versions无效
查看>>
xmanager连接RedHat出错:/usr/X11R6/bin/xterm: No such file or directory
查看>>
我是做SEO的
查看>>
PIG安装配置及案例应用
查看>>
Powershell管理系列(一)Active Direcrtory管理:用户管理
查看>>
使用PsList查看Windows上Oracle的线程等信息
查看>>
2012 定制化产品探讨(周金根).pdf
查看>>
使用智能DNS与多线路由解决教育网服务器费用难题
查看>>
SQL Server 2012 AlwaysOn高可用配置之五:配置仲裁
查看>>
Powershell管理系列(三十九)PowerShell查询和解锁AD账号
查看>>
【实战虚拟化】安全设计之一基本架构
查看>>
LINUX下查看CPU负载的vmstat命令
查看>>
linux里shell中的test代表的意义
查看>>
【VMCloud云平台】SCOM配置(六)-应用深度监控
查看>>
解析linux下磁盘乱序的问题
查看>>
Excel图表之道一突破常规
查看>>
是什么浪费了我工作的时间
查看>>
四大超能力加持,京东家电双11效率致胜
查看>>
关于Golang语言的web编程的实例及常见问题
查看>>