C++学习记录——십삼 List
创始人
2024-06-01 14:44:40
0

文章目录

  • 1、认识List
  • 2、list模拟实现


1、认识List

List实际上是一个双向带头循环列表。

想要遍历List就使用迭代器,方括号只是特定的一种,而迭代器则是通用的。

	list lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);list::iterator it = lt.begin();while (it != lt.end()){cout << *it << " ";++it;}cout << endl;

列表库中提供了一个sort,不用算法库中的sort,是因为算法库中的sort用了快排,需要三数取中,这对于链表来讲很不高效。虽然模板语法上可传任意类型参数,但内部使用迭代器对迭代器有要求,库里接口参数的名字会暗示要传随机迭代器。

迭代器分为三种,单向,双向,随机。单向迭代器例子就是单链表,只能++;链表就是双向迭代器,可++,可–;vector,string的迭代器就是随机迭代器,++ – 以及加减都可用。

要求传单向也可以传双向,随机,要求传双向也可以传随机。

不过列表用sort,显而易见,性能不够好。

2、list模拟实现

写完这篇后我修改了一下,完整代码在这里:

https://gitee.com/kongqizyd/start-some-c-codes-for-learning.c/tree/master/List

先开个头

	templatestruct list_node{list_node* next;list_node* prev;T data;};templateclass list{typedef list_node node;public:list(){head = new node;head->prev = head;head->next = head;}private:node* _head;};

添加插入函数

	struct list_node{list_node* next;list_node* prev;T data;list_node(const T& x):next(nullptr), prev(nullptr), data(x){}};
		void push_back(const T& x){node* tail = head->prev;node* newnode = new node(x);tail->next = newnode;newnode->prev = tail;newnode->next = head;head->prev = newnode;}

如果是单纯的链表结构,那么用一个指针指向一个节点,++就能到下一个节点,但是现在创建的链表不是这样,它的类型是list_node < int >*类型的,解引用后就是list_node,不一定能++正确,它不像链表一样是连续的物理空间。再造一个结构体,里面做些重载函数。

现有一个类list和一个结构体list_node, 在list之前再加一个结构体。

	templatestruct _list_iterator{typedef list_node node;typedef _list_iterator self;node* _node;_list_iterator(node* n):_node(n){}T& operator*(){return _node->data;}self& operator++(){_node = _node->next;return *this;}bool operator!=(const self& s){return _node != s._node;}};

然后在list类里写上测试代码

	void Test_list1(){list lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);list::iterator it = lt.begin();while (it != lt.end()){cout << *it << " ";++it;}cout << endl;for (auto e : lt){cout << e << " ";}cout << endl;}

用struct写是为了公开代码,让代码不受限制。测试代码中的迭代器,begin赋值给it,是浅拷贝,因为我们没写拷贝构造,但是也过了;是因为这里没写析构函数,而且节点不需要释放,因为节点不是迭代器结构体的,而是其他结构体的,所以它没有资格释放。除此之外,前后置+±-以及等于和不等于函数

		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;}bool operator!=(const self& s){return _node != s._node;}

以及begin和end加上const版本和打印函数

		iterator begin(){return iterator(head->next);}iterator begin() const{return iterator(head->next);} iterator end(){return iterator(head);}iterator end() const{return iterator(head);}void print_list(const list& lt){list::iterator it = lt.begin();while (it != lt.end()){(*it) *= 2;cout << *it << " ";++it;}cout << endl;}

但是用const的begin和end构造出来的迭代器里面可以修改数值,这是因为他们构造出来普通迭代器,内容依然可以修改,const修饰的只是指针而已,不是内容。我们要返回const迭代器。

之前已经写过typedef _list_iterator< T > iterator,那么可不可以这样写
typedef const iterator const_iterator。

这就相当于T和T const,也就是这样把迭代器本身变成不可修改了。const迭代器应当可以++,我们要的是cosnt T*。

我们可以这样写,再创建一个结构体,然后把名字中加上const,和原有结构体区分开来。然后改一下这个函数

		const T& operator*(){return _node->data;}

list类里

	templateclass list{typedef list_node node;public:typedef _list_iterator iterator;typedef _list_const_iterator const_iterator;const_iterator end() const{return const_iterator(head);}const_iterator begin() const{return const_iterator(head->next);} 

打印函数里就是这样

list::const_iterator it = lt.begin();

然后把之前那个const结构体去掉,归到原先的结构体里,引入新的模板

	templatestruct _list_iterator{typedef list_node node;typedef _list_iterator self;node* _node;_list_iterator(node* n):_node(n){}Ref operator*(){return _node->data;}
	templateclass list{typedef list_node node;public:typedef _list_iterator iterator;typedef _list_iterator const_iterator;//typedef _list_const_iterator const_iterator;

如果传的是普通的,那就是T和T&。如果是const对象,那就是T和const T&,在_list_iterator结构体那里const T&就是Ref.

继续看迭代器的使用

	struct AA{int _a1;int _a2;AA(int a1, int a2):_a1(a1),_a2(a2){}};void test_list2(){list lt;lt.push_back(AA(1, 1));lt.push_back(AA(2, 2));lt.push_back(AA(3, 3));list::iterator it = lt.begin();while (it != lt.end()){cout << *it << " ";}cout << endl;}

程序会报错,因为it是AA的数据,需要重载<<。但还有别的办法

		list::iterator it = lt.begin();while (it != lt.end()){cout << it->_a1<< ":" << it->_a2 << " ";++it;}cout << endl;

本质是指针的迭代器,这样写貌似看起来更合理,那么就得重载->这个符号。在list_iterator写上这个函数

		T* operator->(){return &_node->data;}

其实本应当是it->->_a1。it一次解引用得到AA*,再次解引用得到AA,不过编译器就会识别为2个。原本的写法是这样:cout << it.operator->()->_a1 << “:” << it.operator->()->_a2 << " ";

如果这里是const,那么又要增加一个模板参数。

完善列表,写插入等函数。

		void push_back(const T& x){/*node* tail = head->prev;node* newnode = new node(x);tail->next = newnode;newnode->prev = tail;newnode->next = head;head->prev = newnode;*/insert(end(), x);}void push_front(const T& x){insert(begin(), x);}void pop_back(){erase(--end());}void pop_front(){erase(begin());}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;}void erase(iterator pos){assert(pos != end());node* prev = pos.node->prev;node* next = pos.node->next;prev->next = next;next->prev = prev;delete pos.node;}

写上clear函数以及要让erase函数传回来现在的位置还有析构函数。

		void clear(){iterator it = begin();while (it != end()){it = erase(it);}}
		iterator erase(iterator pos){assert(pos != end());node* prev = pos.node->prev;node* next = pos.node->next;prev->next = next;next->prev = prev;delete pos.node;return iterator(next);}
		~list(){clear();delete head;head = nullptr;}

迭代器区间函数,顺便改一下初始化。

		void empty_init(){head = new node;head->next = head;head->prev = head;}list(){empty_init();}templatelist(Iterator first, Iterator last){empty_init();while (firse != last){push_back(*first);++first;}}

拷贝构造,两种方式。

		void swap(list& tmp){std::swap(head, tmp.head);}list(const list& lt){/*empty_init();for (auto e : lt){push_back(e);}*/list tmp(lt.begin(), lt.end());swap(tmp)}

重载=

		list& operator=(list lt){swap(lt);return *this;}

结束。

相关内容

热门资讯

122.(leaflet篇)l... 听老人家说:多看美女会长寿 地图之家总目录(订阅之前建议先查看该博客) 文章末尾处提供保证可运行...
育碧GDC2018程序化大世界... 1.传统手动绘制森林的问题 采用手动绘制的方法的话,每次迭代地形都要手动再绘制森林。这...
育碧GDC2018程序化大世界... 1.传统手动绘制森林的问题 采用手动绘制的方法的话,每次迭代地形都要手动再绘制森林。这...
Vue使用pdf-lib为文件... 之前也写过两篇预览pdf的,但是没有加水印,这是链接:Vu...
PyQt5数据库开发1 4.1... 文章目录 前言 步骤/方法 1 使用windows身份登录 2 启用混合登录模式 3 允许远程连接服...
Android studio ... 解决 Android studio 出现“The emulator process for AVD ...
Linux基础命令大全(上) ♥️作者:小刘在C站 ♥️个人主页:小刘主页 ♥️每天分享云计算网络运维...
再谈解决“因为文件包含病毒或潜... 前面出了一篇博文专门来解决“因为文件包含病毒或潜在的垃圾软件”的问题,其中第二种方法有...
南京邮电大学通达学院2023c... 题目展示 一.问题描述 实验题目1 定义一个学生类,其中包括如下内容: (1)私有数据成员 ①年龄 ...
PageObject 六大原则 PageObject六大原则: 1.封装服务的方法 2.不要暴露页面的细节 3.通过r...
【Linux网络编程】01:S... Socket多进程 OVERVIEWSocket多进程1.Server2.Client3.bug&...
数据结构刷题(二十五):122... 1.122. 买卖股票的最佳时机 II思路:贪心。把利润分解为每天为单位的维度,然后收...
浏览器事件循环 事件循环 浏览器的进程模型 何为进程? 程序运行需要有它自己专属的内存空间࿰...
8个免费图片/照片压缩工具帮您... 继续查看一些最好的图像压缩工具,以提升用户体验和存储空间以及网站使用支持。 无数图像压...
计算机二级Python备考(2... 目录  一、选择题 1.在Python语言中: 2.知识点 二、基本操作题 1. j...
端电压 相电压 线电压 记得刚接触矢量控制的时候,拿到板子,就赶紧去测各种波形,结...
如何使用Python检测和识别... 车牌检测与识别技术用途广泛,可以用于道路系统、无票停车场、车辆门禁等。这项技术结合了计...
带环链表详解 目录 一、什么是环形链表 二、判断是否为环形链表 2.1 具体题目 2.2 具体思路 2.3 思路的...
【C语言进阶:刨根究底字符串函... 本节重点内容: 深入理解strcpy函数的使用学会strcpy函数的模拟实现⚡strc...
Django web开发(一)... 文章目录前端开发1.快速开发网站2.标签2.1 编码2.2 title2.3 标题2.4 div和s...