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

做众筹网站需要什么条件服务器如何配置php网站

做众筹网站需要什么条件,服务器如何配置php网站,浏览器网站免费进入,cp网站开发是什么刚开始我是震惊的! 我从未想过[]下居然有逻辑! 从学步开始 曾因会使用a[0]访问数组元素而沾沾自喜 曾固步自封的认为[] ,理应是访问数组的一种方式 天真快乐的同时,认为[]只是一个无情的标识! 所以 当我们写下a[0]时,究竟是为了什么? 是为了找到a[0]对应的值 那么如何…

刚开始我是震惊的!

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

从学步开始

曾因会使用a[0]访问数组元素而沾沾自喜

曾固步自封的认为[] ,理应是访问数组的一种方式

天真快乐的同时,认为[]只是一个无情的标识!

所以

当我们写下a[0]时,究竟是为了什么?

是为了找到a[0]对应的值

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

首先要找到它对应的地址

这样只是能读取

如何能修改其值?(a[0]=5)

当然是返回左值引用

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

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

代码逻辑是怎样的?

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

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

如何确保指针不出界?

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

是否应该考虑const重载?

应该!

#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; // 20list[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;
}

http://mrfarshtey.net/news/155/

相关文章: