C++ 重载 [] 运算符

刚开始我是震惊的!

我从未想过\[\]下居然有逻辑!

从接触程序设计语言开始

曾因会使用a0访问数组元素而沾沾自喜

曾认为\[\] ,理应是访问数组的一种方式

曾认为\[\]只是一个无情的标识!

所以

当我们写下a0时,究竟是为了什么?

是为了找到a0对应的值

那么如何能找到它对应的值?

首先要找到它对应的地址

这样只是能读取

如何能修改其值?(a0=5)

当然是返回左值引用

重载\[\]运算符有何应用场景?

可以像数组一样访问链表类

代码逻辑是怎样的?

index=0 指针不动 返回其数据域

index=1 指针指向下个节点 返回其数据域

如何确保指针不出界?

最后一个节点的指针域是nullptr 如果指针指向nullptr则表示到头了

是否应该考虑const重载?

应该!

cpp 复制代码
#include <iostream>
#include <stdexcept>
 
template <typename T>
class LinkedList
{
private:
    struct Node
    {
        T data;     // 存储的数据
        Node *next; // 指向下一个节点的指针
 
        Node(T val, Node *ptr = nullptr) : data(val), next(ptr) {}
    };
 
    Node *head; // 链表的头节点
 
public:
    LinkedList() : head(nullptr) {}
    ~LinkedList()
    {
        clear();
    }
 
    void add(T value)
    {
        // 在链表末尾添加元素
        if (head == nullptr)
        {
            head = new Node(value);
        }
        else
        {
            Node *temp = head;
            while (temp->next != nullptr)
            {
                temp = temp->next;
            }
            temp->next = new Node(value);
        }
    }
 
    void clear()
    {
        // 清空链表,释放内存
        Node *current = head;
        while (current != nullptr)
        {
            Node *next = current->next;
            delete current;
            current = next;
        }
        head = nullptr;
    }
 
    T &operator[](int index)
    {
        // 重载[]运算符,以便可以使用索引访问元素
        Node *temp = head;
        int count = 0;
        while (temp != nullptr && count < index)
        {
            temp = temp->next;
            ++count;
        }
 
        if (temp == nullptr)
        {
            throw std::out_of_range("Index out of range");
        }
 
        return temp->data; // 返回对应节点的数据引用
    }
 
    // 为了避免在const对象上使用[]运算符时出错,添加const版本的[]运算符
    const T &operator[](int index) const
    {
        Node *temp = head;
        int count = 0;
        while (temp != nullptr && count < index)
        {
            temp = temp->next;
            ++count;
        }
 
        if (temp == nullptr)
        {
            throw std::out_of_range("Index out of range");
        }
 
        return temp->data;
    }
};
 
int main()
{
    LinkedList<int> list;
    list.add(10);
    list.add(20);
    list.add(30);
 
    // 访问和修改元素
    std::cout << list[1] << std::endl; // 20
    list[1] = 50;
    std::cout << list[1] << std::endl; // 50
 
    // 异常处理
    try
    {
        list[3]; // 0 1 2 没有 3
    }
    catch (const std::out_of_range &e)
    {
        std::cout << e.what() << std::endl;
    }
 
    return 0;
}
相关推荐
我是一颗柠檬1 分钟前
【Java项目技术亮点】EXPLAIN深度分析与慢查询治理
android·java·开发语言
luj_17686 分钟前
草酸与烟酸对消化及糖代谢的影响解析
服务器·c语言·开发语言·经验分享·算法
fei_sun11 分钟前
【SystemVerilog】SystemVerilog与C语言的接口
c语言·开发语言
W是笔名21 分钟前
python___容器类型的数据___序列
开发语言·python
☆cwlulu22 分钟前
try-throw-catch异常捕获流程
开发语言·c++
漂亮的摩托28 分钟前
深感一无所长,准备试着从零开始写个富文本编辑器
开发语言·php
要开心吖ZSH35 分钟前
Java事务与MySQL事务的关系及MVCC通俗解析
java·开发语言·mysql·mvcc
王老师青少年编程1 小时前
2026年6月GESP真题及题解(C++五级):排排坐
c++·题解·真题·gesp·五级·2026年6月·排排坐
寻道码路1 小时前
LangChain4j Java AI 应用开发实战(二十六):多模型集成策略 —— OpenAI、DeepSeek、阿里百炼混合使用
java·开发语言·人工智能·ai
面朝大海,春不暖,花不开1 小时前
BPF与eBPF简介:核心概念与观测工具概览
开发语言·php·ebpf·bpf·性能观测