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

【落羽的落羽 C++】list及其模拟实现

在这里插入图片描述

文章目录

  • 一、list介绍
  • 二、list模拟实现
    • 1. 节点
    • 2. 迭代器
    • 3. list

一、list介绍

在这里插入图片描述
list是我们之前学过的带头双向链表的类模板,具有链表的一系列性质,也有多种多样的接口便于使用,使用方法与vector大体相似:

函数接口说明
list()构造空的list,只有头结点,头结点的前后指针指向自己
begin返回第一个元素(即头结点的下一个)的迭代器
end返回最后一个元素下一个位置(即头结点)的迭代器
empty判断list是否为空,是返回true,否则返回false
front返回第一个节点中值的引用
back返回最后一个节点中值的引用
push_front头插
push_back尾插
pop_front头删
pop_back尾删
insert插入
erase删除

除此之外,list还有几个特殊的接口:

  • unique,删除重复值在这里插入图片描述
    这个接口能删除list中的重复值,但前提是这个list中数据必须是有序的,如果不是有序的则结果会出错。所以一般都要在调用算法库中的sort排序后再用unique。
    在这里插入图片描述
  • remove,移除指定元素
    在这里插入图片描述
    很好理解
    在这里插入图片描述
  • splice,移动
    在这里插入图片描述
    splice既可以用于不同list间的数据转移,也可以用于一个list中的数据调整位置:
    在这里插入图片描述
    在这里插入图片描述

二、list模拟实现

list是一个类模板,那么我们要模拟实现它,显然要先实现出list结点的结构,再实现list类。
除此之外,list的迭代器更加特殊,不像string、vector,由于它们的底层是数组,在内存空间中是连续的,所以它们的迭代器可以用指针直接实现,它们的迭代器可以使用指针的一系列运算符如++、+、*、<等。而list的底层在内存中是不连续的,而且每一个元素都存在各自独立的结点中,如果直接用指针做迭代器,指针的那些操作符就是不合法的。所以我们不能直接用指针做迭代器,但是又想让迭代器有指针的效果便于使用,解决方法就是,用类来封装实现迭代器

1. 节点

在使用list时,用户一般都不会直接接触到它的节点,所以节点的结构没有必要用访问限定符修饰了,直接用struct实现就行。当然,它还是一个模板,因为存储数据类型会多种多样。

template<class T>
struct list_node
{list_node* _next;list_node* _prev;T _data;list_node(const T& x = T()): _next(nullptr), _prev(nullptr), _data(x){ }
};

2. 迭代器

用类封装迭代器的目的是,能重载相关的运算符,便于使用

template<class T>
struct list_iterator
{typedef list_node<T> Node;typedef list_iterator<T> Self; //将迭代器暂且命名为Self便于类内使用Node* _node; //这个迭代器指向的节点list_iterator(Node* node):_node(node){ }T& 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) const{return _node != s._node;}bool operator==(const Self& s) const{return _node == s._node;}
};

这是普通的iterator,但list的迭代器有普通的iterator和const_iterator,前者可以修改引用的内容,后者不可以修改引用的内容。在具体实现上它们区别之一是重载*时,iterator中是T& operator*(),const_iterator中是const T& operator*(),这样解引用const_iterator出的结果就无法修改:

template<class T>
struct list_const_iterator
{typedef list_node<T> Node;typedef list_const_iterator<T> Self; //将迭代器暂且命名为Self便于类内使用Node* _node; //这个迭代器指向的节点list_const_iterator(Node* node):_node(node){ }const T& 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) const{return _node != s._node;}bool operator==(const Self& s) const{return _node == s._node;}
};

虽说可以这样写,但是不觉得代码太冗余了吗?有人已经能察觉到了,这里关于iterator和const_iterator,完全可以利用模板写在一起:我们在模板类型中增加一个Ref,代表这个迭代器是普通或是const版本,然后在重载*处写成Ref operator*(),其余出不用修改。这样,迭代器如果是const版本的,给Ref传const T&类型,是普通版本的则传T&类型,巧妙完成了这个问题:

template<class T, class Ref>
struct list_iterator
{typedef list_node<T> Node;typedef list_iterator<T, Ref> Self; //将迭代器暂且命名为Self便于类内使用Node* _node; //这个迭代器指向的节点list_iterator(Node* node):_node(node){}Ref 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) const{return _node != s._node;}bool operator==(const Self& s) const{return _node == s._node;}};

然后,又有一个新的问题:
如果list存的数据是自定义类型,此时我们也会想利用->操作符用迭代器访问到自定义结构中的成员:

struct A
{int a1;char a2;
};list<A> lt;
lt.push_back({1, 'a'});
list<A>::iterator it = lt.begin();
cout << it->a1 << it->a2 << endl;

所以,迭代器中也要重载->运算符。但同样的,const版本迭代器不能对指向内容进行修改,还是和上面一样,区分const版本和普通版本迭代器最好用模板来解决,给iterator的模板类型中增加第三个类型Ptr,这样,迭代器如果是const版本的,给Ptr传const T*类型,是普通版本的则传T*类型:

tip:一定不能给const版本迭代器传成T& const或T* const类型,因为迭代器本身一定能改变引用或指向的目标,是能修改的。const T&和const T*才是不能修改引用或指向内容的。

template<class T, class Ref, class Ptr>
struct list_iterator
{Ptr operator->(){return &_node->_data;}//......
};

此时再返回来看上面的例子:

struct A
{int a1;char a2;
};list<A> lt;
lt.push_back({1, 'a'});
list<A>::iterator it = lt.begin();
cout << it->a1 << it->a2 << endl;

却感觉怪怪的,it指向的是一个A类型元素,a1和a2是这个A类型元素的成员,理论上不应该用两个->操作符才能访问到吗。这就是一个特殊处理,写两个->很冗余,于是就规定就这样写一个->,理解就好。不过,如果显式写出运算符重载,就应该写成两个->了:如it.operator->()->a1

在这里插入图片描述

综上所述,我们的list的迭代器的最终实现是:

template<class T, class Ref, class Ptr>
struct list_iterator
{typedef list_node<T> Node;typedef list_iterator<T, Ref, Ptr> Self; //将迭代器暂且命名为Self便于类内使用Node* _node; //这个迭代器指向的节点list_iterator(Node* node):_node(node){}Ptr operator->(){return &_node->_data;}Ref 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) const{return _node != s._node;}bool operator==(const Self& s) const{return _node == s._node;}
};

接下来,在下面的list本体中,只需
typedef list_iterator<T, T&, T*> iterator;(普通迭代器)
typedef list_iterator<T, const T&, const T*> const_iterator;(const迭代器)。

3. list

最后需要我们实现的,就剩下list的常用接口了:

	template<class T>class list{typedef list_node<T> Node;private:Node* _head;size_t _size;public:typedef list_iterator<T, T&, T*> iterator;typedef list_iterator<T, const T&, const T*> const_iterator;iterator begin(){return iterator(_head->_next);}iterator end(){return iterator(_head);}const_iterator begin() const{return const_iterator(_head->_next);}const_iterator end() const{return const_iterator(_head->_next);}list(){_head = new Node;_head->_next = _head;_head->_prev = _head;_size = 0;}//利用{}进行构造list(initializer_list<T> il){_head = new Node;_head->_next = _head;_head->_prev = _head;_size = 0;for (auto& e : il){push_back(e);}}list(list<T>& lt){_head = new Node;_head->_next = _head;_head->_prev = _head;_size = 0;for (auto& e : lt){push_back(e);}}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;}~list(){clear();delete _head;_head = nullptr;}void clear(){auto it = begin();while (it != end()){it = erase(it);}}size_t size(){return _size;}//在pos位置前插入一个新结点void insert(iterator pos, const T& x){Node* cur = pos._node;Node* prev = cur->_prev;Node* newnode = new Node(x);prev->_next = newnode;newnode->_prev = prev;newnode->_next = cur;cur->_prev = newnode;_size++;}void push_back(const T& x){insert(end(), x);}void push_front(const T& x){insert(begin(), x);}//删除pos位置的结点iterator erase(iterator pos){assert(pos != end());Node* cur = pos._node;Node* prev = cur->_prev;Node* next = cur->_next;prev->_next = next;next->_prev = prev;delete cur;_size--;return next;//实际应该是return iterator(next),不过编译器会隐式类型转换}void pop_back(){erase(--end());}void pop_front(){erase(begin());}};

最后,以下是list的完整模拟实现,放在我们自己的list.h中:

#pragma once
#include<assert.h>
#include<iostream>
using namespace std;namespace lydly
{template<class T>struct list_node{list_node* _next;list_node* _prev;T _data;list_node(const T& x = T()): _next(nullptr), _prev(nullptr), _data(x){ }};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){ }Ptr operator->(){return &_node->_data;}Ref 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) const{return _node != s._node;}bool operator==(const Self& s) const{return _node == s._node;}};template<class T>class list{typedef list_node<T> Node;private:Node* _head;size_t _size;public:typedef list_iterator<T, T&, T*> iterator;typedef list_iterator<T, const T&, const T*> const_iterator;iterator begin(){return iterator(_head->_next);}iterator end(){return iterator(_head);}const_iterator begin() const{return const_iterator(_head->_next);}const_iterator end() const{return const_iterator(_head->_next);}list(){_head = new Node;_head->_next = _head;_head->_prev = _head;_size = 0;}//利用{}进行构造list(initializer_list<T> il){_head = new Node;_head->_next = _head;_head->_prev = _head;_size = 0;for (auto& e : il){push_back(e);}}list(list<T>& lt){_head = new Node;_head->_next = _head;_head->_prev = _head;_size = 0;for (auto& e : lt){push_back(e);}}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;}~list(){clear();delete _head;_head = nullptr;}void clear(){auto it = begin();while (it != end()){it = erase(it);}}size_t size(){return _size;}//在pos位置前插入一个新结点void insert(iterator pos, const T& x){Node* cur = pos._node;Node* prev = cur->_prev;Node* newnode = new Node(x);prev->_next = newnode;newnode->_prev = prev;newnode->_next = cur;cur->_prev = newnode;_size++;}void push_back(const T& x){insert(end(), x);}void push_front(const T& x){insert(begin(), x);}//删除pos位置的结点iterator erase(iterator pos){assert(pos != end());Node* cur = pos._node;Node* prev = cur->_prev;Node* next = cur->_next;prev->_next = next;next->_prev = prev;delete cur;_size--;return next;//实际应该是return iterator(next),不过编译器会隐式类型转换}void pop_back(){erase(--end());}void pop_front(){erase(begin());}};
}

在这里插入图片描述
本篇完,感谢阅读

http://www.xdnf.cn/news/299629.html

相关文章:

  • On the Biology of a Large Language Model——论文学习笔记——拒答和越狱
  • 华为私有协议Hybrid
  • 5月6日日记
  • QtGUI模块功能详细说明,图像处理(三)
  • 目标检测(Object Detection)研究方向常用数据集简单介绍
  • 【Bootstrap V4系列】学习入门教程之 组件-卡片(Card)高级用法
  • 数据初步了解
  • 论文速读:《CoM:从多模态人类视频中学习机器人操作,助力视觉语言模型推理与执行》
  • 电池热管理CFD解决方案,为新能源汽车筑安全防线
  • TikTok 矩阵账号运营实操细节:打造爆款矩阵
  • SpringBoot整合Kafka、Flink实现流式处理
  • 三种信号本振
  • Redis 7.0中5种新特性及实战应用
  • 【ArcGISPro】创建要素和刷新数据库后卡顿
  • 浔川AI 第二次内测报告
  • 数据可视化与分析
  • Flutter开发IOS蓝牙APP的大坑
  • 购物数据分析
  • 云境天合水陆安全漏电监测仪—迅速确定是否存在漏电现象
  • OS7.【Linux】基本指令入门(6)
  • FPGA实战项目1——坦克大战
  • HarmonyOS 5.0 分布式数据协同与跨设备同步​​
  • 在sheel中运行Spark
  • 【quantity】0 README.md文件
  • Linux服务之nginx中高级配置
  • C++笔记-二叉搜索树(包括key,key/value搜索场景等)
  • 一个基于Netty和WebRTC的实时通讯系统
  • 大数据应用开发和项目实战-电商双11美妆数据分析
  • LangChain入门(六)Agent
  • 演讲学习的总结