C++ —— list - 教程

1.list 的介绍及使用

1.1 list 的介绍

数据结构的双向链表是参考 list 来写的。

与vector最大的不同是:list 不支持下标+[ ]的访问。

void test_list1()
{
	list lt1;
	lt1.push_back(1);
	lt1.push_back(2);
	lt1.push_back(3);
	lt1.push_back(4);
	list::iterator it1 = lt1.begin();
	while (it1 != lt1.end())
	{
		cout << *it1 <<" ";
		++it1;
	}
	cout << endl;
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
}
int main()
{
	test_list1();
	return 0;
}

运行结果:

1.2 list的使用

1.2.1 assign是一种赋值:

void test_list2()
{
	list lt1;
	lt1.push_back(1);
	lt1.push_back(2);
	lt1.push_back(3);
	lt1.push_back(4);
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
	lt1.assign(10, 1);
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
	lt1.assign(2, 1);
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
}

运行结果:

1.2.2 emplace:

在结构或者是自定义类型中才会与push_back有差异(只是了解)

void test_list3()
{
	list lt1;
	//只能插入int
	lt1.push_back(1);
	lt1.push_back(2);
	//可以插入int
	lt1.emplace_back(3);
	lt1.emplace_back(4);
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
	list lt2;
	A aa1(2, 2);
	lt2.push_back(aa1);     //有名对象
	lt2.push_back(A(2,2));  //匿名对象
	lt2.push_back({2,2});   //隐式类型转换
	lt2.emplace_back(aa1);
	lt2.emplace_back(A(2, 2));
	//lt2.emplace_back({ 2,2 });   //不能用隐式类型转换,没办法推导类型
	//更高效——直接构造
	lt2.emplace_back(2, 2);
}

1.2.3 remove:可以理解为是find和erase的一个结合,有这个值就删掉

void test_list4()
{
	list lt1 = { 10,2,6,32,2,4,5,2,2 };
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
	lt1.remove(122);
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
	lt1.remove(5);
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
	//删除所有的2
	lt1.remove(2);
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
}

运行结果:

1.2.4 sort:算法库中有sort为什么list中还要单独提供sort?

在这里运行时是不支持的,因为算法库里面的 sort 里面的迭代器是用减的:

但是链表的迭代器支持++、-- 但是不支持 -。这里涉及到迭代器的详细知识:

算法是借助迭代器作用到容器上面的,不同的算法是对迭代器有不同的要求的:

算法库中的算法为什么不支持?

算法库中的算法其实是一个快排,快排必然要用到 '-' 之类的。在list中,算法库不支持sort所以我们自己实现一个sort,链表不能用快排堆排之类的。归并的效率较高,用的就是归并(很复杂)。(了解就好)

默认是升序,如果要调整为降序的话会涉及到仿函数。

void test_list5()
{
	list lt1 = { 10,2,6,32,2,4,5,2,2 };
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
	//不支持,因为sort要求要用随机迭代器
	//sort(lt1.begin(), lt1.end());
	// < 升序
	lt1.sort();
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
	//// > 降序
	//greater gt;
	//lt1.sort(gt);
	lt1.sort(greater());
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
}

list 中的 sort 知道就可以了,能不用就不用,数据量小浅浅的用一用,数据量大的就能不用就不用。

1.2.5 unique:去重算法(O(n)),只能去掉连续重复的值

想要达到去掉重复值,只保留一个值的话先用sort进行排序,让重复的值都排在一起,然后用unique来进行对重复值的删除:

void test_list6()
{
	list lt1 = { 10,3,3,3,3,6,32,2,4,5,2,2 };
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
	lt1.sort();
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
	lt1.unique();
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
}

运行结果:

unique的原理是双指针:

1.2.6 splice:进行转移

void test_list7()
{
	list lt1 = { 1,2,3,4,5,6 };
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
	//将1调整到尾部去:
	lt1.splice(lt1.end(), lt1, lt1.begin());
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
}

运行结果:

2. list和迭代器的简单定义:

#pragma once
namespace ysy
{
	template
	struct list_node
	{
		list_node* _next;
		list_node* _prev;
		T _data;
		list_node(const T& x=T())
			:_next(nullptr)
			,_prev(nullptr)
			,_data(x)
		{ }
	};
	template
	struct list_iterator
	{
		typedef list_node Node;
		Node* _node;
		struct list_iterator(Node* node)
			:_node(node)
		{}
		T& operator*()
		{
			return _node->_data;
		}
		list_iterator& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		bool operator!=(const list_iterator& it)
		{
			return _node != it._node;
		}
	};
	template
	//什么时候用struct?——定义节点的时候
	class list
	{
		typedef list_node Node;
	public:
		typedef list_iterator iterator;
		iterator begin()
		{
			//return iterator(_head->_next);  //匿名对象
			 iterator it(_head->_next);  //有名对象
			 return it;
		}
		iterator end()
		{
			return iterator(_head);
		}
		list()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}
		void push_back(const T& x)
		{
			Node* newnode = new Node(x);
			Node* tail = _head->_prev;
			tail->_next = newnode;
			newnode->_prev = tail;
			_head->_prev = newnode;
			newnode->_next = _head;
		}
	private:
		Node* _head;
	};
}

用一个类型去封装一个结点的指针,因为要重载运算符才能达到我们自己的一些目的

结点的指针可以构造一个迭代器,迭代器实现的内核还是结点的指针。

迭代器功能进行进一步的加强:

void test_ysylist2()
{
	ysy::list lt2;
	lt2.push_back({ 1,1 });
	lt2.push_back({ 2,2 });
	lt2.push_back({ 3,3 });
	lt2.push_back({ 4,4 });
	ysy::list::iterator it = lt2.begin();
	while (it != lt2.end())
	{
		//cout << *it << " ";  //不能cout,因为这里调用operator*,node.data是一个A类型的对象
							   //A类型的对象不支持流插入,也可以自己再写上A类型的流插入
							   // 如果不写上对应A类型的流插入的话,就应该像下面那样写:
		cout << (*it)._a1 << ":" << (*it)._a2 << endl;
		++it;
	}
	cout << endl;
}

运行结果:

但是这样的话就会面临第二个问题:

cout << (*it)._a1 << ":" << (*it)._a2 << endl;

(*it) 返回 data 数据,data数据是一个A类型的数据, (*it)._a1这样就可以取到它里面的数据,A*取数据是用箭头:-> ,所以要重载一个箭头这样的运算符:

下一个问题,假设现在有一个const对象。要遍历这个容器要用const迭代器来遍历,const迭代器怎么写?

	template
	struct list_const_iterator
	{
		typedef list_node Node;
		Node* _node;
		struct list_const_iterator(Node* node)
			:_node(node)
		{
		}
		const T& operator*()
		{
			return _node->_data;
		}
		const T* operator->()
		{
			return &_node->_data;
		}
		list_const_iterator& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		bool operator!=(const list_const_iterator& it)
		{
			return _node != it._node;
		}
		bool operator==(const list_const_iterator& it)
		{
			return _node == it._node;
		}
	};
	template
	//什么时候用struct?——定义节点的时候
	class list
	{
		typedef list_node Node;
	public:
		typedef list_iterator iterator;
		typedef list_const_iterator const_iterator;
		iterator begin()
		{
			//return iterator(_head->_next);  //匿名对象
			iterator it(_head->_next);  //有名对象
			return it;
		}
		iterator end()
		{
			return iterator(_head);
		}
		const_iterator begin() const
		{
			return const_iterator(_head->_next);  //有名对象
		}
		const_iterator end() const
		{
			return const_iterator(_head);
		}
		list()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}
		void push_back(const T& x)
		{
			Node* newnode = new Node(x);
			Node* tail = _head->_prev;
			tail->_next = newnode;
			newnode->_prev = tail;
			_head->_prev = newnode;
			newnode->_next = _head;
		}
	private:
		Node* _head;
	};
}
void test_ysylist2()
{
    ysy::list lt1;
	lt1.push_back(1);
	lt1.push_back(2);
	lt1.push_back(3);
	lt1.push_back(4);
	Print(lt1);
}

运行结果:

以上两个类的重复度很高,还有其他的办法吗?——当然有,将它们两个合成一个模板!

typedef list_iterator iterator;
typedef list_iterator const_iterator;
	template
	struct list_iterator
	{
	typedef list_node Node;
	typedef list_iterator Self;
	Node* _node;
	struct list_iterator(Node* node)
		:_node(node)
	{
	}
	Ref operator*()
	{
		return _node->_data;
	}
	Ptr operator->()
	{
		return &_node->_data;
	}
	Self& operator++()
	{
		_node = _node->_next;
		return *this;
	}
	bool operator!=(const Self& it)
	{
		return _node != it._node;
	}
	bool operator==(const Self& it)
	{
		return _node == it._node;
	}

vector在之前的stl_30代码中迭代器使用的就是原生指针,但是现在库里面的迭代器都不是用原生指针实现的:不用原生指针实现迭代器,可以根据自己的需求更精准地去控制它的行为

#include
int main()
{
	cout << typeid(vector::iterator).name() << endl;
	return 0;
}

3.链表的增删查改:

3.1 链表的插入:不存在迭代器失效的问题

		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;
		}

3.2 链表的删除:会导致迭代器的失效,pos已经野指针了,所以要更新一下迭代器

		iterator erase(iterator pos)
		{
			//不能删除头结点
			assert(pos != end());
			Node* cur = pos._node;
			Node* nextNode = cur->_next;
			Node* prevNode = cur->_prev;
			prevNode->_next = nextNode;
			nextNode->_prev = prve;
			delete cur;
			return iterator(nextNode);
		}

3.3 尾插、头插、头删、尾删的实现:都是依靠基础的删除和插入的操作的实现的。

void push_back(const T& x)
{
	/*Node* newnode = new Node(x);
	Node* tail = _head->_prev;
	tail->_next = newnode;
	newnode->_prev = tail;
	_head->_prev = newnode;
	newnode->_next = _head;*/
	insert(end(),x);
}
void push_front(const T& x)
{
	insert(begin(), x);
}
void pop_front()
{
	erase(begin());
}
void pop_back()
{
	erase(--end());
}
void test_ysylist3()
{
	ysy::list lt1;
	lt1.push_back(1);
	lt1.push_back(2);
	lt1.push_back(3);
	lt1.push_back(4);
	for (auto& e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
	lt1.push_front(5);
	lt1.push_front(6);
	for (auto& e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
	lt1.pop_back();
	lt1.pop_back();
	lt1.pop_front();
	lt1.pop_front();
	for (auto& e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
}

运行结果:

3.4 size的实现:

	template
	//什么时候用struct?——定义节点的时候
	class list
	{
		typedef list_node Node;
	public:
		/*typedef list_iterator iterator;
		typedef list_const_iterator const_iterator;*/
		typedef list_iterator iterator;
		typedef list_iterator const_iterator;
		list()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}
		size_t size() const
		{
			return _size;
		}
		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;
		}
		iterator erase(iterator pos)
		{
			//不能删除头结点
			assert(pos != end());
			Node* cur = pos._node;
			Node* nextNode = cur->_next;
			Node* prevNode = cur->_prev;
			prevNode->_next = nextNode;
			nextNode->_prev = prevNode;
			delete cur;
			--_size;
			return iterator(nextNode);
		}
	private:
		Node* _head;
		size_t _size;
	};
}

3.5 list的深浅拷贝:

void test_ysylist4()
{
	ysy::list lt1;
	lt1.push_back(1);
	lt1.push_back(2);
	lt1.push_back(3);
	lt1.push_back(4);
	for (auto& e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
	ysy::list lt2(lt1);
	lt1.clear();
	for (auto& e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
	for (auto& e : lt2)
	{
		cout << e << " ";
	}
	cout << endl;
}
void empty_init()
{
	_head = new Node;
	_head->_next = _head;
	_head->_prev = _head;
	_size = 0;
}
list()
{
	empty_init();
}
//深拷贝 lt2(lt1)
list(const list& lt)
{
	empty_init();
	for (auto& e : lt)
	{
		push_back(e);
	}
}
~list()
{
	clear();
	delete _head;
	_head = nullptr;
}
void clear()
{
	iterator it = begin();
	while (it != end())
	{
		it = erase(it);  //迭代器失效的问题
	}
}

运行结果:

3.6 赋值的实现:

	ysy::list lt3;
	lt3.push_back(10);
	lt3.push_back(20);
	lt3.push_back(30);
	lt3.push_back(40);
	lt1 = lt3;
	for (auto& e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;

运行结果:

调试结果:

lt1和lt3指向的头结点都是同一个,说明就是浅拷贝。

3.7 深拷贝实现的代码:

		//lt1 = lt3
		list& operator=(list lt)   //现代写法精华:这里用传值
		{
			swap(lt);
			return *this;
		}
		void swap(list& lt)
		{
			std::swap(_head, lt._head);
			std::swap(_size, lt._size);
		}

3.8 列表初始化的实现:

ysy::list lt3 = {10,20,30,40};
	list(initializer_list lt)
	{
		empty_init();
		for (auto& e : lt)
		{
			push_back(e);
		}
	}

4. list迭代器失效

此处的迭代器暂时理解成类似于指针,迭代器失效即迭代器所指向的结点的无效,即该结点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器是不会受到影响。

5. list 和 vector的对比

vector 与 list 都是STL中非常重要的序列式容器,由于两个容器的底层结构不同,导致其特性以及应用场景不同,其主要不同如下:

vectorlist

底层结构

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

posted @ 2026-02-03 12:03  yangykaifa  阅读(0)  评论(0)    收藏  举报