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

C++中list各种基本接口的模拟实现

一、基本框架

        这里我们使用三个类,分别实现链表节点本身、迭代器的构建以及各种接口的实现与提供

二、难点

        因为我们的链表在内存中不是连续存储的,所以不能够使用原生指针作为迭代器,所以需要单独封装一个类,来实现迭代器比如解引用、++、--、->、!=等运算符的重载,其实就是将原生指针版迭代器的各种运算符重载使其能够支持类

        在一个就是关于iterator和const_iterator的实现问题,比较简单的方法可以是先实现一个iterator,在复制一份,将其中的解引用和箭头更改为const版本;但翻阅原码可以发现原码以一种类模版的方式实现:即让编译器生成对应的类。两种方法在效率上没有区别,但第二种相对更加简洁。

三、还有一个可以注意一下的小点是struct默认是公有,class默认是私有,因此迭代器作为非常常用的类可以将它写为struct,访问更加方便

using namespace std;
//list就是带头双向循环链表
//链表在空间上的存储是不连续的,不能使用原生指针作为迭代器
namespace wjl
{template <class T>class list_node{public:T _data;list_node<T>* _next;list_node<T>* _prev;list_node(const T& data = T()): _data(data), _next(nullptr), _prev(nullptr){}};template <class T, class Ref, class Ptr>struct list_iterator//默认是公有{typedef list_node<T> Node;typedef list_iterator<T, Ref, Ptr> Self;Node* _node;list_iterator(Node* node):_node(node){}Ref operator*(){return _node->_data;}Ptr operator->(){return &_node->_data;}Self& operator++(){_node = _node->_next;return *this;}Self& operator++(int){//这里迭代器的指针就是需要浅拷贝,不需要写构造函数和析构函数Self tmp(*this);_node = _node->_next;return tmp;}Self& operator--(){_node = _node->_prev;return *this;}Self& operator--(int){Self tmp(*this);_node = _node->_prev;return tmp;}bool operator!=(const Self& s){return _node != s._node;}};template <class T>class list{typedef list_node<T> Node;public://这里原码所采取的方式是提供类模版给编译器,让编译器生成两个类/*typedef list_iterator<T> iterator;typedef list_const_iterator<T> const_iterator;*///这里就是编译器的写法:用类模版实例化出不同的迭代器类型,本质是模版复用实例化typedef list_iterator<T, T&, T*> iterator;typedef list_iterator<T, const T&, const T*> const_iterator;//这里将哨兵位后第一个节点视为头结点iterator begin(){//最传统的写法iterator it(_head->_next);return it;}const_iterator begin() const{return _head->_next;}//这里将哨兵位视为尾节点iterator end(){//单参数构造函数支持隐式类型转换return _head;//或者采用匿名结构体//return iterator(_head);}const_iterator end() const{return _head;}void empty_init(){_head = new Node;_head->_next = _head;_head->_prev = _head;_size = 0;}list(){_head = new Node;_head->_next = _head;_head->_prev = _head;_size = 0;}list(const list<T>& lt){empty_init();for (auto& e : lt){push_back(e);}}list(initializer_list<int> lt){empty_init();//这里范围for不知道lt类型一定要用别名for (auto& e : lt){push_back(e);}}~list(){clear();delete _head;_head = nullptr;cout << "~list()" << endl;}void clear(){auto it = begin();while (it != end()){it = erase(it);//这里it就是下一个位置的迭代器}}void swap(list<T>& lt){std::swap(_head, lt._head);std::swap(_size, lt._size);}//赋值运算符重载都可以使用现代写法list<T>& operator=(list<T> lt){swap(lt);return *this;}void push_back(const T& x){////通过_head找到尾节点//Node* newnode = new Node(x);//这里前面的list_node要写构造函数,否则编译器会将其视为类型转换////_head->_prev->_next = newnode;//newnode->_prev = _head->_prev;//newnode->_next = _head;//_head->_prev = newnode;//++_size;insert(end(), x);}void push_front(const T& x){insert(begin(), x);}iterator insert(iterator pos, const T& x){//在pos之前插入Node* cur = pos._node;Node* prev = cur->_prev;Node* newnode = new Node(x);newnode->_prev = prev;newnode->_next = cur;prev->_next = newnode;cur->_prev = newnode;_size++;return newnode;}iterator erase(iterator pos){assert(pos != end());Node* next = (pos._node)->_next;Node* prev = (pos._node)->_prev;prev->_next = next;next->_prev = prev;delete pos._node;_size--;return next;}void pop_back(){erase(--end());}void pop_front(){erase(begin());}int size() const{return _size;}bool empty() const{return _size == 0;}private://这里的头结点就是哨兵位,不放数据。Node* _head;int _size;};

文章转载自:
http://seditionary.sxnf.com.cn
http://boutique.sxnf.com.cn
http://watkins.sxnf.com.cn
http://arcturus.sxnf.com.cn
http://fashionist.sxnf.com.cn
http://rewake.sxnf.com.cn
http://alb.sxnf.com.cn
http://benzal.sxnf.com.cn
http://width.sxnf.com.cn
http://dowse.sxnf.com.cn
http://quibblesome.sxnf.com.cn
http://tetrazzini.sxnf.com.cn
http://chemigraphy.sxnf.com.cn
http://wagonload.sxnf.com.cn
http://manganin.sxnf.com.cn
http://aquiherbosa.sxnf.com.cn
http://antitank.sxnf.com.cn
http://louis.sxnf.com.cn
http://biogeny.sxnf.com.cn
http://timberjack.sxnf.com.cn
http://lineable.sxnf.com.cn
http://dimorph.sxnf.com.cn
http://medievalist.sxnf.com.cn
http://myology.sxnf.com.cn
http://autarkist.sxnf.com.cn
http://botanical.sxnf.com.cn
http://cebu.sxnf.com.cn
http://kweilin.sxnf.com.cn
http://refrigerator.sxnf.com.cn
http://reascend.sxnf.com.cn
http://www.dtcms.com/a/280353.html

相关文章:

  • 【Java代码审计(2)】MyBatis XML 注入审计
  • 153.在 Vue 3 中使用 OpenLayers + Cesium 实现 2D/3D 地图切换效果
  • java中的接口
  • JavaScript 动态访问嵌套对象属性问题记录
  • HarmonyOS-ArkUI: Web组件加载流程1
  • 暴力破解:攻破系统的终极密钥
  • Rust指针选择
  • 安装带GPU的docker环境
  • 20250715使用荣品RD-RK3588开发板在Android13下接入USB3.0接口的红外相机
  • 【I3D 2024】Deblur-GS: 3D Gaussian Splatting from Camera Motion Blurred Images
  • 记录一条面试sql题目
  • JS中async/await功能介绍和使用演示
  • 普通字符类型和new String有什么区别
  • 使用JS编写动态表格
  • 【env环境】rtthread5.1.0使用fal组件
  • AI的外挂知识库,RAG检索增强生成技术
  • 【PTA数据结构 | C语言版】将表达式树转换成中缀表达式
  • 数仓面试题
  • 2025最新国产用例管理工具评测:Gitee Test、禅道、蓝凌测试、TestOps 哪家更懂研发协同?
  • docker停止所有容器和删除所有镜像
  • 从一道题目(阿里2014 Crackme_2)开启unidbg还原算法入门(转载)
  • 强化学习书籍
  • vscode 打开c++文件注释乱码
  • 分布式存储之Ceph使用指南--部署篇(未完待续)
  • Claude 背后金主亚马逊亲自下场,重磅发布 AI 编程工具 Kiro 现已开启免费试用
  • 【交叉编译报错】fatal: not a git repository (or any of the parent directories): .git
  • 分布式全局唯一ID生成:雪花算法 vs Redis Increment,怎么选?
  • 内存的基础相关知识,什么是内存,内存管理
  • 死锁问题以及读写锁和自旋锁介绍【Linux操作系统】
  • Spring 中 @Component和@Bean注解的区别