当前位置: 首页 > 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());}};
}

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

相关文章:

  • 某公园楼栋自由曲面薄壳结构自动化监测
  • 薪技术|0到1学会性能测试第45课-apache调优技术
  • Apache Doris 使用指南:从入门到生产实践
  • 2025 后端自学UNIAPP【项目实战:旅游项目】2、安装下载引用前端UI框架:uview-plus
  • 从试错到智能决策:Python与强化学习优化自动驾驶策略
  • 高等数学第五章---定积分(§5.2微积分基本定理)
  • Labview培训5_以空间换时间的数据采集策略介绍
  • 消除AttributeError: module ‘ttsfrd‘ has no attribute ‘TtsFrontendEngine‘报错输出的记录
  • 在sheel中运行Spark
  • Python之内省与反射应用
  • 前端-HTML+CSS+JavaScript+Vue+Ajax概述
  • Axure : 列表分页、 列表翻页
  • 链表结构深度解析:从单向无头到双向循环的实现全指南
  • VBA ListBox/ComboBox 响应鼠标滚轮操作
  • FPGA实战项目1——坦克大战
  • 深入解析 Linux/Unix 通信机制:从原理到观测实践
  • 美团后端开发一面
  • C++23 新利器:深入解析栈踪迹库 (P0881R7)
  • 【Bootstrap V4系列】学习入门教程之 组件-卡片(Card)高级用法
  • Blender插件机制设计与Python实现
  • 今晚上海地铁多条线路加开定点加班车,2号线运营至次日2时
  • “五一”假期预计全社会跨区域人员流动量累计14.67亿人次
  • 马斯克的“星舰基地”成为新城镇,首任市长为SpaceX员工
  • 港股5月迎开门红,恒生科指涨3%,欧股开盘全线上扬
  • 经营业绩持续稳中向好,国铁集团2024年度和2025年一季度财务决算公布
  • 中国武术协会原主席张耀庭逝世,曾促成电影《少林寺》拍摄