当前位置: 首页 > news >正文

C++设计模式+异常处理

 

 

 

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
#include <stdexcept>  // 包含异常类

using namespace std;

// 该作业要求各位写一个链表
// 所以myList类里面需要一个真正的链表

template <class T>
class myList{
public:
    struct Node{
        // 数据域
        T val;
        // 指针域
        Node* next;
        Node* prev;
    };

    // 迭代器
    class iterator{
    public:
        Node* cur;  // head地址
        iterator(Node* node = nullptr) 
        : cur(node) {}

        T& operator*() {
            return cur->val;
        }

        iterator& operator++() {  // 前缀 ++
            if (cur) 
                cur = cur->next;
            return *this;
        }

        iterator& operator++(int) {  // 后缀 ++
            if (cur) 
                cur = cur->next;
            return *this;
        }

        bool operator!=(const iterator& other) const {
            return cur != other.cur;
        }
    };

    myList();
    void push_back(const T& val);
    myList& operator<<(const T& val);
    T& operator[](int index);
    int size();

    iterator begin() {
        return iterator(head->next);
    }

    iterator end() {
        return iterator(NULL);
    }

private:
    Node* head;  // 真正的链表(链表头头节点)
    Node* tail;  // 链表尾节点
    int count;   // 元素数量
};

template <typename T>
myList<T>::myList(){
    head = new Node;
    head->next = NULL;
    head->prev = NULL;
    tail = head;  // 只有头节点的情况下,尾节点即使是头节点
    count = 0;
}

template <typename T>
void myList<T>::push_back(const T& val){
    Node* newnode = new Node;
    newnode->val = val;
    newnode->next = NULL;
    newnode->prev = tail;

    tail->next = newnode;

    tail = newnode;
    count++;
}

template <typename T>
myList<T>& myList<T>::operator<<(const T& val){
    push_back(val);
    return *this;
}

template <typename T>
T& myList<T>::operator[](int index){
    if (index < 0 || index >= count) {
        throw std::out_of_range("超出范围");  // 如果索引超出范围,抛出异常
    }
    Node* p = head->next;
    for (int i = 0; i < index; i++) {
        p = p->next;
    }
    return p->val;
}

template <typename T>
int myList<T>::size(){
    return count;
}

int main(int argc, const char** argv){
    try {
        myList<int> l;
        l << 1 << 3 << 5 << 7 << 9;  // 插入5个数

        // 输出链表的元素
        myList<int>::iterator it = l.begin();
        for (it; it != l.end(); ++it) {
            cout << *it << endl;
        }

        // 尝试访问链表中的第六个元素,应该抛出异常
        cout << l[5] << endl;  // 此行会抛出异常

    } catch (const std::out_of_range& e) {
        cout <<e.what() << endl;  // 捕获并输出异常信息
    }

    return 0;
}

http://www.dtcms.com/a/122691.html

相关文章:

  • 21 天 Python 计划:MySQL 数据库初识
  • LangChain使用大语言模型构建强大的应用程序
  • 开源模型应用落地-模型上下文协议(MCP)-从数据孤岛到万物互联(一)
  • Linux 实时查看 CUDA 显卡的使用情况命令
  • 基于形状补全和形态测量描述符的腓骨游离皮瓣下颌骨重建自动规划|文献速递-深度学习医疗AI最新文献
  • 【Linux】Linux 操作系统 - 03 ,初步指令结尾 + shell 理解
  • Qt中的信号与槽及其自定义
  • mysql Creating sort index
  • 博物馆小程序怎么做?从0到1打造数字化文化窗口
  • openEuler欧拉系统配置local的yum源
  • 蓝桥杯-蓝桥幼儿园(Java-并查集)
  • 【KWDB 创作者计划】_KWDB:开源引领数据库创新变革
  • 多图超详细:Docker安装知识库AI客服RAGFlow的详细步骤、使用教程及注意事项:
  • Vue接口平台学习五——测试环境页面
  • 关于量化交易在拉盘砸盘方面应用的部分思考
  • HTML 是什么?网页创建的核心标记语言
  • 如何开发英语在线训练小程序:从0到1的详细步骤
  • spring-cloud-starter-alibaba-seata使用说明
  • C++ RAII 的用途及业务代码实现案例
  • 20周年系列|美创科技再度入围「年度高成长企业」系列榜单
  • 鸿蒙NEXT开发Preferences工具类(ArkTs)
  • Python生成器与列表的对照使用详解
  • 2025高教社杯全国大学生数学建模竞赛——5个月备赛规划
  • Spring Boot 启动后自动执行 Service 方法终极指南
  • 正则表达式补充——python
  • leetcode_707. 设计链表_java
  • Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
  • 游戏引擎学习第211天
  • 毫米波测试套装速递!高效赋能5G/6G、新材料及智能超表面(RIS)研发
  • Stable Diffusion + CelebA-Dialog 数据集:不同数据集文本标签格式不一致?我的实验记录