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

C++day9

思维导图

 

牛客练习

 练习:

将我们写的 myList 迭代器里面 operator[] 和 operator++ 配合异常再写一遍

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>

using namespace std;

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

template <class T>
class myList{
public:
    struct Node{
        T val;
        Node* next;
        Node* prev;
    };
    class iterator{
        private:
            Node* p;
        public:
            iterator(Node* p=NULL):p(p){};
            T& operator*()
            {
                return p->val;
            }
            bool operator!=(const iterator& r)
            {
                return p!=r.p;
            }
            iterator& operator++(int)
            {
                p=p->next;
                return *this;
            }
            iterator& operator++()
            {
                p=p->next;
                return *this;
            }
    };

    myList();
    void push_back(const T& val);
    myList& operator<<(const T& val);
    T& operator[](int index);
    int size();
    iterator begin();
    iterator end();
private:
    Node* head; //真正的链表(链表头头节点)
    Node* tail; // 链表尾节点
    int count;
};

template <typename T>
typename myList<T>::iterator myList<T>::begin()
{
    iterator it(head->next);
    return it;
}

template <typename T>
typename myList<T>::iterator myList<T>::end()
{
    iterator it(tail->next);
    return it;
}
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 0
    return *this;
}

template <typename T>
T& myList<T>::operator[](int index){
    Node* p = head->next;
    try{
        for(int i=0;i<index;i++){
            p = p->next;
            if(p==head)
            {
                cout<<"error"<<endl;
                break;
            }
        }
        throw bad_alloc();
    }catch(const bad_alloc& e){                                             
        cerr<<e.what()<<endl;
    }
    return p->val;
}

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

int main(int argc,const char** argv){
    myList<int> l;
    l << 1 << 3 << 5 << 7 << 9;
    myList<int>::iterator it=l.begin();
    for(it;it!=l.end();it++)
    {
        cout<<*it<<"";
    }
    cout<<endl;
    for(auto ele:l)
    {
        cout<<ele<<"";
    }
    cout<<endl;
    cout<<l[5]<<endl;
    return 0;
}

实现效果: myList<int> l; l << 1 << 3 << 5 << 7 << 9 总共5个数 如果此时,执行了 l[0 ~ 4] 正常,如果执行了 l[5~n] 自动抛出异常 也就是说,我们需要在 operator[] 函数里面,判断传入的下标是否合法,是否在范围内,如果不合法立刻抛出异常,注意函数内部只负责抛出异常

相关文章:

  • 电阻的额定功率与额定电压详解
  • 【什么是动态链接?这里的动态是什么意思?链接了什么?】
  • 【人工智能】如何通过精准提示工程实现完美的珠宝首饰展示
  • 企业使用Excel开展数据分析限制和建议完整版
  • 基于大模型的非阵发性室性心动过速风险预测与诊疗方案研究报告
  • Android中Jetpack设计理念、核心组件 和 实际价值
  • PGA 简介
  • 单元测试原则之——不要过度模拟
  • 刮板机断链保护装置的运行机制
  • JavaScript高阶函数的定义
  • VFlash 工具使用教程
  • 基于视觉语言模型的机器人实时探索系统!ClipRover:移动机器人零样本视觉语言探索和目标发现
  • Spring入门概念 以及入门案例
  • 使用 Mapped Pinned Memory 优化 YOLOv10 TensorRT 推理速度:Zero-Copy 技术实战
  • 大学论文书写规范与格式说明
  • CJ92930在消费类领域的运用
  • Java服务端开发基石:深入理解Spring IoC与依赖注入 (DI)
  • 替换jeecg图标
  • QT面试题:内存管理与对象生命周期
  • Docker+MySQL的主从架构同步数据的方法
  • 上海网站建设的网站/搜索引擎有哪些
  • 济阳做网站公司/广告做到百度第一页
  • 免费打开的网站/百度网址安全中心怎么关闭
  • 个人做动漫资源网站/app投放推广
  • 网站建设林晓东/杭州seo营销
  • 周口市建设职工培训中心网站/收录优美图片官网