C++——观察者模式,3个工厂模式,迭代器模式,异常处理 ...

打印 上一主题 下一主题

主题 1675|帖子 1675|积分 5025


  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <unistd.h>
  5. #include <sstream>
  6. #include <vector>
  7. #include <memory>
  8. using namespace std;
  9. class myException:public exception{
  10.         private:
  11.         public:
  12.                 const char* what()const noexcept{
  13.                         return "越界访问";
  14.                 }
  15. };
  16. template <class T>
  17. class myList{
  18. public:
  19.         struct Node{
  20.                 T val;
  21.                 Node* next;
  22.                 Node* prev;
  23.         };
  24.         class iterator{
  25.                 private:
  26.                         Node* ptr;
  27.                 public:
  28.                         iterator(Node* ptr=NULL);
  29.                         T& operator*();
  30.                         bool operator!=(const iterator& r);
  31.                         iterator& operator++(int);
  32.                         iterator& operator++();
  33.         };
  34.         myList();
  35.         void push_back(const T& val);
  36.         myList& operator<<(const T& val);
  37.         T& operator[](int index);
  38.         int size();
  39.         iterator begin();
  40.         iterator end();
  41. private:
  42.         Node* head; //真正的链表(链表头头节点)
  43.         Node* tail; // 链表尾节点
  44.         int count;
  45. };
  46. //===========================================
  47. //迭代器代码
  48. template <typename T>
  49. myList<T>::iterator::iterator(Node* ptr)
  50.         :ptr(ptr){}
  51. template <typename T>
  52. T& myList<T>::iterator::operator*(){
  53.         return ptr->val;
  54. }
  55. template <typename T>
  56. bool myList<T>::iterator::operator!=(const iterator& r){
  57.         return ptr!=r.ptr;
  58. }
  59. template <typename T>
  60. typename myList<T>::iterator& myList<T>::iterator::operator++(int){
  61.         if(ptr==NULL)
  62.         {
  63.                 throw myException();
  64.         }
  65.         ptr=ptr->next;
  66.         return *this;
  67. }
  68. template <typename T>
  69. typename myList<T>::iterator& myList<T>::iterator::operator++(){
  70.         if(ptr==NULL)
  71.         {
  72.                 throw myException();
  73.         }
  74.         ptr=ptr->next;
  75.         return *this;
  76. }
  77. template <typename T>
  78. typename myList<T>::iterator myList<T>::begin(){
  79.         iterator it(head->next);
  80.         return it;
  81. }
  82. template <typename T>
  83. typename myList<T>::iterator myList<T>::end(){
  84.         iterator it(tail->next);
  85.         return it;
  86. }
  87. template <typename T>
  88. myList<T>::myList(){
  89.         head = new Node;
  90.         head->next = NULL;
  91.         head->prev = NULL;
  92.         tail = head; // 只有头节点的情况下,尾节点即使头节点
  93.         count = 0;
  94. }
  95. template <typename T>
  96. void myList<T>::push_back(const T& val){
  97.         Node* newnode = new Node;
  98.         newnode->val = val;
  99.         newnode->next = NULL;
  100.         newnode->prev = tail;
  101.         tail->next = newnode;
  102.         tail = newnode;
  103.         count ++;
  104. }
  105. template <typename T>
  106. myList<T>& myList<T>::operator<<(const T& val){
  107.         push_back(val);
  108.         // return 0
  109.         return *this;
  110. }
  111. template <typename T>
  112. T& myList<T>::operator[](int index){
  113.         if(index<0 || index>=count){
  114.                 throw myException();
  115.         }
  116.         Node* p = head->next;
  117.         for(int i=0;i<index;i++){
  118.                 p = p->next;
  119.         }
  120.         return p->val;
  121. }
  122. template <typename T>
  123. int myList<T>::size(){
  124.         return count;
  125. }
  126. int main(int argc,const char** argv){
  127.         myList<int> l;
  128.         l << 1 << 3 << 5 << 7 << 9;
  129.         try{
  130.                 cout << l[5] << endl;
  131.         }catch(const myException& e){
  132.                 cout << "捕获异常:" << e.what() <<endl;
  133.         }
  134.         try{
  135.                 myList<int>::iterator it=l.end();
  136.                 ++it;
  137.         }catch(const myException& e){
  138.                 cout << "捕获异常:" << e.what() <<endl;
  139.         }
  140.                
  141.         return 0;
  142. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

缠丝猫

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表