STL之list

1. list的介绍和使用

1.1 list的介绍

  1. list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
  2. list的底层是带头双向循环链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向 其前一个元素和后一个元素。
  3. list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高效。
  4. 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率 更好。
  5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list 的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间 开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这 可能是一个重要的因素)

1.2 list的使用

1.2.1 list的构造

构造函数(constructor 接口说明
list() 构造空的list
list (size_type n, const value_type& val = value_type()) 构造的list中包含n个值为val的元素
list (const list& x) 拷贝构造函数
list (InputIterator first, InputIterator last) 用[first, last)区间中的元素构造list

1.2.2 list iterator的使用

从实现结构的角度,迭代器分为三类:

  1. 单向。++ 例如:单链表、unordered_map、unordered_set
  2. 双向。++、-- 例如:双链表、map、set、queue
  3. 随机。++、--、+、- 例如:vector、string、deque
函数声明 接口说明
begin() + end() 返回第一个元素的迭代器 + 返回最后一个元素的下一个位置的迭代器
rbegin() + rend() 返回第一个元素的reverse_iterator,即end位置,返回最后一个元素下一个位置的reverse_iterator,即begin()位置

注意:

  1. begin与end为正向迭代器,对迭代器执行++操作,迭代器向后移动
  2. rbegin(end)与rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动

使用举例:

cpp 复制代码
void test_list1()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	list<int>::iterator it = lt.begin();
	while (it != lt.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
	list<int>::reverse_iterator rit = lt.rbegin();
	while (rit != lt.rend())
	{
		cout << *rit << " ";
		rit++;
	}
	cout << endl;
}

运行截图:

1.2.3 list capacity

函数声明 接口说明
empty 检测list是否为空,是返回true,否则返回false
size 返回list中有效节点的个数

1.2.4 list element access

函数声明 接口说明
front 返回list的第一个节点中值的引用
back 返回list的最后一个节点中值的引用

1.2.5 list modifiers

函数声明 接口说明
push_front 在list首元素前插入值为val的元素
pop_front 删除list中第一个元素
push_back 在list尾部插入值为val的元素
pop_back 删除list中最后一个元素
insert 在list position 位置中插入值为val的元素
erase 删除list position位置的元素
swap 交换两个list中的元素
clear 清空list中的有效元素

代码举例:

cpp 复制代码
list<int> lt;
lt.push_back(1);//尾插
lt.push_front(10);//头插
lt.pop_back();//尾删
lt.pop_front();//头删
lt.insert(lt.begin(), 1);//任意位置插入
lt.erase(lt.begin());//任意位置删除
list<int> l1;
list<int> l2;
l1.swap(l2);//交换l1和l2中的元素
lt.clear();

1.2.6 list的sort接口

使用举例:

cpp 复制代码
void test_list2()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(6);
	lt.push_back(7);
	lt.push_back(4);
	lt.push_back(8);
	lt.push_back(10);
	lt.push_back(9);
	lt.sort();
	list<int>::iterator it = lt.begin();
	while (it != lt.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
}

运行截图:

1.2.7 list的unique

作用:去除重复元素。前提是必须是有序的链表。

使用举例:

cpp 复制代码
void test_list2()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(6);
	lt.push_back(7);
	lt.push_back(4);
	lt.push_back(8);
	lt.push_back(10);
	lt.push_back(9);
	lt.push_back(1);
	lt.push_back(4);
	lt.push_back(7);
	lt.push_back(6);
	lt.push_back(8);

	lt.sort();
	lt.unique();
	list<int>::iterator it = lt.begin();
	while (it != lt.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
}

运行截图:

1.2.8 list的splice

注意:在将x插入到*this之后,x中的节点都消失了,即都被添加到了*this中。

使用举例:

cpp 复制代码
void test_list3()
{
	list<int> l1;
	l1.push_back(1);
	l1.push_back(2);
	l1.push_back(3);
	l1.push_back(4);

	list<int> l2;
	l2.push_back(10);
	l2.push_back(20);
	l2.push_back(30);
	l2.push_back(40);

	auto it = l1.begin();
	it++;
	l1.splice(it, l2);
	cout << "l1:";
	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
	cout << "l2:";
	for (auto e : l2)
	{
		cout << e << " ";
	}
	cout << endl;
}

运行截图:

2.list的模拟实现

2.1 list的代码实现

cpp 复制代码
template<class T>
struct list_node
{
	list_node<T>* _next;
	list_node<T>* _prev;
	T _data;

	list_node(const T& val = T())
		:_next(nullptr)
		, _prev(nullptr)
		, _data(val)
	{}
};

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 &(operator*());
		//return *_node->_data;
	}


	self& operator++()
	{
		_node = _node->_next;
		return *this;
	}

	self& operator--()
	{
		_node = _node->_prev;
		return *this;
	}

	self operator++(int)
	{
		self tmp(*this);
		_node = _node->_next;
		return tmp;
	}
	self operator--(int)
	{
		self tmp(*this);
		_node = _node->_prev;
		return tmp;
	}
	bool operator!=(const self& it)
	{
		return _node != it._node;
	}
	bool operator==(const self& it)
	{
		return _node == it._node;
	}

};

template<class T>
class mylist
{
	typedef list_node<T> Node;

public:
	typedef __list_iterator<T, T&, T*> iterator;
	typedef __list_iterator<T, const T&, const T*> const_iterator;
	常规写法
	//mylist(const mylist<T>& lt)
	//{
	//	_head = new Node;
	//	_head->_next = _head;
	//	_head->_prev = _head;
	//	for (auto e : lt)
	//	{
	//		push_back(e);
	//	}
	//}
	//现代写法
	mylist(const mylist<T>& lt)
	{
		empty_init();
		mylist<T> tmp(lt.begin(), lt.end());
		swap(tmp);
	}
	mylist<T>& operator=(mylist<T> lt)
	{
		swap(lt);
		return *this;
	}

	template<class InputIterator>
	mylist(InputIterator first, InputIterator last)
	{
		empty_init();
		_head = new Node();
		_head->_next = _head;
		_head->_prev = _head;
		while (first != last)
		{
			push_back(*first);
			++first;
		}
	}
	void empty_init()
	{
		_head = new Node();
		_head->_next = _head;
		_head->_prev = _head;
	}
	void swap(mylist<T>& lt)
	{
		std::swap(_head, lt._head);
	}
	iterator begin()
	{
		return iterator(_head->_next);
	}
	const_iterator begin()const
	{
		return const_iterator(_head->_next);
	}
	iterator end()
	{
		return iterator(_head);
	}
	const_iterator end()const
	{
		return const_iterator(_head);
	}
	mylist()
	{
		_head = new Node();
		_head->_next = _head;
		_head->_prev = _head;
	}

	void push_back(const T& x)
	{
		//Node* tail = _head->_prev;
		//Node* newnode = new Node(x);
		tail newnode _head
		//tail->_next = newnode;
		//newnode->_prev = tail;
		//newnode->_next = _head;
		//_head->_prev = newnode;

		insert(end(), x);
	}
	void push_front(const T& x)
	{
		insert(begin(), x);
	}
	iterator erase(iterator pos)
	{
		assert(pos != end());
		Node* cur = pos._node;
		Node* prev = cur->_prev;
		Node* next = cur->_next;
		//prev next
		prev->_next = next;
		next->_prev = prev;
		delete cur;
		return iterator(next);
	}
	void pop_back()
	{
		erase(--end());
	}
	void pop_front()
	{
		erase(begin());
	}
	//插入在pos位置之前
	//prev newnode cur
	iterator insert(iterator pos, const T& x)
	{
		Node* newNode = new Node(x);
		Node* cur = pos._node;
		Node* prev = cur->_prev;
		prev->_next = newNode;
		newNode->_prev = prev;
		newNode->_next = cur;
		cur->_prev = newNode;

		return iterator(newNode);
	}

	~mylist()
	{
		clear();
		delete _head;
		_head = nullptr;
	}
	void clear()
	{
		iterator it = begin();
		while (it != end())
		{
			it = erase(it);
		}
	}
private:
	Node* _head;
};

2.2 list 需要注意的点

2.2.1 ->的重载

问:为什么我们要重载->?

答:当mylist存储的自定义类型,例如下面的类型时:

cpp 复制代码
struct AA
{
    int _a1;
    int _a2;
}

然后我们想要支持下面的行为(迭代器模拟的是指针,按道理来说应该支持下面的行为):

cpp 复制代码
cout << it->_a1 << it->_a2 << endl;

所以我们需要对这个进行重载:

cpp 复制代码
T* operator->()
{
    return &(operator*());
    //operator*()即_data,所以上面等价于&(_node->_data)
}

问:那么it->_a1是如何访问的呢?

答:

it->等价于it.operator->(),其类型为AA*,所以这个地方还是少了一个->,即接下来的访问方式应该是这样的(it.operator->)->_a1,即真正的表达式应该是这样的:it->->_a1

但是编译器为了可读性进行了优化,即it->_a1相当于it->->_a1,优化以后,省略了一个箭头。

相关推荐
王伯爵13 分钟前
5G核心网(5GC)中PCC(Policy and Charging Control,策略与计费控制)
服务器·网络·5g
rqtz2 小时前
【C++指针】搭建起程序与内存深度交互的桥梁(下)
开发语言·c++·指针
小陈的进阶之路3 小时前
数据结构(并查集,图)
数据结构·c++·算法
落淼喵_G3 小时前
【windows搭建lvgl模拟环境(一)之VSCode】
ide·windows·vscode
SNAKEpc121383 小时前
在MFC中使用Qt(三):通过编辑项目文件(.vcxproj)实现Qt的自动化编译流程
c++·qt·mfc
Brown.alexis3 小时前
服务器本地镜像上传到阿里云远程镜像仓库
运维·服务器·阿里云
Edward-tan4 小时前
Django 生成 ssl 安全证书,切换 https、wss协议(daphne 、nginx)
运维·服务器·django
会挖坑的石头4 小时前
C语言术语
c语言
ahahahahaha23335 小时前
相似度计算 ccf-csp 2024-2-2
数据结构·c++·算法
试水年华5 小时前
鸿蒙Next-集成HmRouter的路由模式
linux·运维·服务器·华为·harmonyos·ark-ts语言