• 【STL】list常见用法及模拟实现(附完整源码)


    前言

    这篇文章我们继续STL中容器的学习,这篇文章要讲解的是list。

    1. list介绍及使用

    1.1 list介绍

    list文档
    在这里插入图片描述
    list的底层实现就是数据结构学过的带头双向循环链表:
    在这里插入图片描述

    1.2 list使用

    我们来看一下几个常用的接口:

    首先看一下构造函数:
    在这里插入图片描述
    这里几个都是我们熟悉的,默认构造、n个val构造、迭代器区间构造以及拷贝构造。
    我们再来看一下迭代器:
    在这里插入图片描述
    我相信之前的文章对迭代器的介绍已经很详细了这里我们就不过多赘述了。

    我们再来看一下修改操作:
    在这里插入图片描述

    这里list与string和vector区别在于:
    list没有重载[],也就是说我们要遍历和访问list,就只能用迭代器。迭代器才是通用的方式,所有的容器都可以用迭代器,而[]只是针对特定容器的特殊方式。

    遍历

    int main()
    {
        list<int> l;
        l.push_back(1);
        l.push_back(2);
        l.push_back(3);
        l.push_back(3);
        l.push_back(5);
        for (auto it = l.begin(); it != l.end(); ++it)
            cout << *it << " ";
        cout << endl;
    
        for (auto e : l)
            cout << e << " ";
        cout << endl;
    
        for (auto rit = l.rbegin(); rit != l.rend(); ++rit)
            cout << *rit << " ";
        cout << endl;
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    在这里插入图片描述

    我们再来看几个之前没怎么用过的接口:
    在这里插入图片描述
    这里的splice ,它可以把链表的一部分转移到另一个链表:
    在这里插入图片描述
    remove就是删除指定的元素:
    在这里插入图片描述
    merge可以合并两个有序链表:
    在这里插入图片描述

    2. list模拟实现

    2.1 迭代器功能分类

    1、单向迭代器:只能++,不能- -。例如单链表,哈希表;
    2、双向迭代器:既能++也能--。例如双向链表;
    3、随机访问迭代器:能++ - -,也能+-。例如vector和string。
    迭代器是内嵌类型(内部类或定义在类里)

    2.2 list迭代器模拟实现
    2.2.1 普通迭代器

    在模拟实现string、vector时是使用的原生指针,没有将迭代器用类进行封装,但是STL标准库也是用类封装了迭代器。而在模拟实现list迭代器时,不能用原生指针了,因为list的节点地址是不连续的。

    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->_val;
    		}
    
    		Ptr operator->()
    		{
    			return &_node->_val;
    		}
    
    		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& it) const
    		{
    			return _node != it._node;
    		}
    
    		bool operator==(const self& it) const
    		{
    			return _node == it._node;
    		}
    	};
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    2.2.2 const迭代器

    我们先来看一下错误写法:
    在这里插入代码片typedef __list_iterator iterator; const list::iterator it=lt.begin();

    在STL库中是通过类模板多给一个参数来实现,这样,同一份类模板就可以生成两种不同的类型的迭代器

    template<class T>
    	class list
    	{
    		typedef list_node<T> Node;
    
    	public:
    		typedef __list_iterator<T, T&, T*> iterator;
    		typedef __list_iterator<T, const T&, const T*> const_iterator;
    
    
    		iterator begin()
    		{
    			//return _head->_next;
    			return iterator(_head->_next);
    		}
    
    		iterator end()
    		{
    			return _head;
    			//return iterator(_head);
    		}
    
    		const_iterator begin() const
    		{
    			//return _head->_next;
    			return const_iterator(_head->_next);
    		}
    
    		const_iterator end() const
    		{
    			return _head;
    			//return const_iterator(_head);
    		}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    3. list和vector区别

    vectorlist
    底 层 结 构动态顺序表,一段连续空间带头结点的双向循环链表
    随 机 访 问支持随机访问,访问某个元素效率O(1)不支持随机访问,访问某个元素效率O(N)
    插 入 和 删 除任意位置插入和删除效率低,需要搬移元素,时间复杂度为O(N),插入时有可能需要增容,增容:开辟新空间,拷贝元素,释放旧空间,导致效率更低任意位置插入和删除效率高,不需要搬移元素,时间复杂度为O(1)
    空 间 利 用 率底层为连续空间,不容易造成内存碎片,空间利用率高,缓存利用率高底层结点动态开辟,小节点容易造成内存碎片,空间利用率低,缓存利用率低
    迭 代 器原生态指针对原生态指针进行封装
    迭 代 器 失 效在插入元素时,要给所有的迭代器重新赋值,因为插入元素有可能会导致重新扩容,致使原来迭代器失效,删除时,当前迭代器需要重新赋值否则会失效带头结点的双向循环链表
    使 用 场 景需要高效存储,支持随机访问,不关心插入删除效率大量插入和删除操作,不关心随机访问

    4. 源码

    list.h

    #include
    using namespace std;
    namespace w
    {
    	template<class T>
    	struct list_node
    	{
    		list_node<T>* _next;
    		list_node<T>* _prev;
    		T _val;
    
    		list_node(const T& val = T())
    			:_next(nullptr)
    			, _prev(nullptr)
    			, _val(val)
    		{}
    	};
    
    	// typedef __list_iterator iterator;
    	// typedef __list_iterator const_iterator;
    	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->_val;
    		}
    
    		Ptr operator->()
    		{
    			return &_node->_val;
    		}
    
    		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& it) const
    		{
    			return _node != it._node;
    		}
    
    		bool operator==(const self& it) const
    		{
    			return _node == it._node;
    		}
    	};
    
    
    	template<class T>
    	class list
    	{
    		typedef list_node<T> Node;
    
    	public:
    		typedef __list_iterator<T, T&, T*> iterator;
    		typedef __list_iterator<T, const T&, const T*> const_iterator;
    
    
    		iterator begin()
    		{
    			//return _head->_next;
    			return iterator(_head->_next);
    		}
    
    		iterator end()
    		{
    			return _head;
    			//return iterator(_head);
    		}
    
    		const_iterator begin() const
    		{
    			//return _head->_next;
    			return const_iterator(_head->_next);
    		}
    
    		const_iterator end() const
    		{
    			return _head;
    			//return const_iterator(_head);
    		}
    
    		void empty_init()
    		{
    			_head = new Node;
    			_head->_prev = _head;
    			_head->_next = _head;
    
    			_size = 0;
    		}
    
    		list()
    		{
    			empty_init();
    		}
    
    		// lt2(lt1)
    		list(const list<T>& lt)
    		//list(const list& lt)
    		{
    			empty_init();
    
    			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)
    		//list& operator=(list lt)
    		{
    			swap(lt);
    
    			return *this;
    		}
    
    		~list()
    		{
    			clear();
    
    			delete _head;
    			_head = nullptr;
    		}
    
    		void clear()
    		{
    			iterator it = begin();
    			while (it != end())
    			{
    				it = erase(it);
    			}
    
    			_size = 0;
    		}
    
    		void push_back(const T& x)
    		{
    			insert(end(), x);
    		}
    
    		void push_front(const T& x)
    		{
    			insert(begin(), x);
    		}
    
    		void pop_back()
    		{
    			erase(--end());
    		}
    
    		void pop_front()
    		{
    			erase(begin());
    		}
    
    		// pos位置之前插入
    		iterator insert(iterator pos, const T& x)
    		{
    			Node* cur = pos._node;
    			Node* prev = cur->_prev;
    			Node* newnode = new Node(x);
    
    			prev->_next = newnode;
    			newnode->_next = cur;
    
    			cur->_prev = newnode;
    			newnode->_prev = prev;
    
    			++_size;
    
    			return newnode;
    		}
    
    		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;
    		}
    
    		size_t size()
    		{
    			return _size;
    		}
    
    	private:
    		Node* _head;
    		size_t _size;
    	};
    
    	void Print(const list<int>& lt)
    	{
    		list<int>::const_iterator it = lt.begin();
    		while (it != lt.end())
    		{
    		
    			cout << *it << " ";
    			++it;
    		}
    		cout << endl;
    	}
    
    	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
  • 相关阅读:
    2175. The Change in Global Rankings
    Paddle GPU版本需要安装CUDA、CUDNN
    Vite HMR API
    Redis从入门到精通(二)- 入门篇
    Git01下载安装+与SVN的区别+实操
    基于数据库的全文检索实现
    神经网络的基本结构
    JWT(1): JWT介绍
    毕业设计2349基于jsp的网上订餐系统【程序源码+文档+调试运行】
    web前端期末大作业:青岛旅游网页主题网站设计——青岛民俗4页 HTML+CSS 民俗网页设计与制作 web网页设计实例作业 html实训大作业
  • 原文地址:https://blog.csdn.net/weixin_69423932/article/details/133484490