【C++】——list的介绍及模拟实现

1. 前言

我们之前已经学习了string和vector,今天我们来学习C++中的另一个容器——list,list的底层是带头双向循环链表。

2. list的介绍

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

3. list的常用接口

3.1 list的构造函数

函数名称 功能说明
list (size_type n, const value_type& val = value_type()) 构造的list中包含n个值为val的元素
list() 构造空的list
list (const list& x) 拷贝构造函数
list (InputIterator first, InputIterator last) 用[first, last)区间中的元素构造list
在这里插入代码片void Test1()
{
    
    
	list<int> lt1;
	list<int> lt2(10, 1);
	list<int> lt3(lt2);
}

在这里插入图片描述

3.2 iterator的使用

此处,可暂时将迭代器理解成一个指针,该指针指向list中的某个节点。

函数名称 功能说明
begin+end 返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器
rbegin+rend 返回第一个元素的reverse_iterator,即end位置,返回最后一个元素下一个位置的reverse_iterator,即begin位置
void Test2()
{
    
    
	list<int> lt1;
	lt1.push_back(1);
	lt1.push_back(2);
	lt1.push_back(3);
	lt1.push_back(4);
	lt1.push_back(5);

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

	it = lt1.begin();
	while (it != lt1.end())
	{
    
    
		(*it)++;
		cout << *it << " ";
		it++;
	}
	cout << endl;

	for (auto e : lt1)
	{
    
    
		cout << e << " ";
	}
	cout << endl;
}

在这里插入图片描述

3.3 list的空间管理

函数名称 功能说明
empty 检测list是否为空,是返回true,否则返回false
size 返回list中有效节点的个数
void Test3()
{
    
    
	list<int> lt1;
	list<int> lt2;
	lt2.push_back(1);
	lt2.push_back(2);
	lt2.push_back(3);
	lt2.push_back(4);
	lt2.push_back(5);

	cout << lt1.empty() << endl;
	cout << lt2.size() << endl;

}

在这里插入图片描述

3.4 list的结点访问

函数名称 功能说明
front 返回list的第一个节点中值的引用
back 返回list的最后一个节点中值的引用
void Test4()
{
    
    
	list<int> lt1;
	lt1.push_back(1);
	lt1.push_back(2);
	lt1.push_back(3);
	lt1.push_back(4);
	lt1.push_back(5);

	cout << lt1.front() << endl;
	cout << lt1.back() << endl;

	int& a = lt1.front();
	int& b = lt1.back();
	a++;
	b++;

	for (auto e : lt1)
	{
    
    
		cout << e << " ";
	}
	cout << endl;
}

在这里插入图片描述

3.5 list的增删查改

函数名称 功能说明
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中的有效元素
void Test5()
{
    
    
	list<int> lt1;
	lt1.push_front(1);
	lt1.push_front(2);
	lt1.push_front(3);
	lt1.push_front(4);
	lt1.push_front(5);
	for (auto e : lt1)
	{
    
    
		cout << e << ' ';
	}
	cout << endl;

	lt1.pop_front();
	lt1.pop_front();
	lt1.pop_front();
	for (auto e : lt1)
	{
    
    
		cout << e << ' ';
	}
	cout << endl;

	list<int> lt2;
	lt2.push_back(1);
	lt2.push_back(2);
	lt2.push_back(3);
	lt2.push_back(4);
	lt2.push_back(5);
	for (auto e : lt2)
	{
    
    
		cout << e << ' ';
	}
	cout << endl;

	lt2.pop_back();
	lt2.pop_back();
	lt2.pop_back();
	for (auto e : lt2)
	{
    
    
		cout << e << ' ';
	}
	cout << endl;

	list<int> lt3;
	lt3.push_back(1);
	lt3.push_back(2);
	lt3.push_back(3);
	lt3.push_back(4);
	lt3.push_back(5);
	list<int>::iterator pos = find(lt3.begin(), lt3.end(), 3);
	lt3.insert(pos, 10);//迭代器不会失效
	lt3.insert(pos, 20);
	for (auto e : lt3)
	{
    
    
		cout << e << ' ';
	}
	cout << endl;

	//lt3.erase(pos);//迭代器会失效
	pos = lt3.erase(pos);
	pos = lt3.erase(pos);
	for (auto e : lt3)
	{
    
    
		cout << e << ' ';
	}
	cout << endl;

	lt1.swap(lt3);
	for (auto e : lt1)
	{
    
    
		cout << e << ' ';
	}
	cout << endl;
	for (auto e : lt3)
	{
    
    
		cout << e << ' ';
	}
	cout << endl;

	lt2.clear();
	for (auto e : lt2)
	{
    
    
		cout << e << ' ';
	}
	cout << endl;

}

在这里插入图片描述

4. list迭代器失效的问题

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

迭代器失效:

void Test6()
{
    
    
	list<int> lt1;
	lt1.push_back(1);
	lt1.push_back(2);
	lt1.push_back(3);
	lt1.push_back(4);
	lt1.push_back(5);
	list<int>::iterator it = lt1.begin();
	while (it != lt1.end())
	{
    
    
		//错误——迭代器失效
		//erase()函数执行后,it所指向的节点已被删除,因此it无效,在下一次使用it时,必须先给其赋值
		lt1.erase(it);
		++it;
	}
}

在这里插入图片描述

修改后:

void Test7()
{
    
    

	list<int> lt1;
	lt1.push_back(1);
	lt1.push_back(2);
	lt1.push_back(3);
	lt1.push_back(4);
	lt1.push_back(5);
	list<int>::iterator it = lt1.begin();
	while (it != lt1.end())
	{
    
    
		it = lt1.erase(it);//正确
	}
}

5. list模拟实现

#pragma once

#include <assert.h>

namespace fiora
{
    
    
	template<class T>
	struct list_node
	{
    
    
		T _data;
		list_node<T>* _next;
		list_node<T>* _prev;

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

	// typedef __list_iterator<T, T&, T*>             iterator;
	// typedef __list_iterator<T, const T&, const T*> const_iterator;

	// 像指针一样的对象
	template<class T, class Ref, class Ptr>
	struct __list_iterator
	{
    
    
		typedef list_node<T> Node;
		typedef __list_iterator<T, Ref, Ptr> iterator;

		typedef bidirectional_iterator_tag iterator_category;
		typedef T value_type;
		typedef Ptr pointer;
		typedef Ref reference;
		typedef ptrdiff_t difference_type;


		Node* _node;

		__list_iterator(Node* node)
			:_node(node)
		{
    
    }

		bool operator!=(const iterator& it) const
		{
    
    
			return _node != it._node;
		}

		bool operator==(const iterator& it) const
		{
    
    
			return _node == it._node;
		}

		Ref operator*()
		{
    
    
			return _node->_data;
		}

		//T* operator->() 
		Ptr operator->()
		{
    
    
			return &(operator*());
		}

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

		// it++
		iterator operator++(int)
		{
    
    
			iterator tmp(*this);
			_node = _node->_next;
			return tmp;
		}

		// --it
		iterator& operator--()
		{
    
    
			_node = _node->_prev;
			return *this;
		}

		// it--
		iterator operator--(int)
		{
    
    
			iterator tmp(*this);
			_node = _node->_prev;
			return tmp;
		}
	};

	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;

		const_iterator begin() const
		{
    
    
			return const_iterator(_head->_next);
		}

		const_iterator end() const
		{
    
    
			return const_iterator(_head);
		}

		iterator begin()
		{
    
    
			return iterator(_head->_next);
		}

		iterator end()
		{
    
    
			return iterator(_head);
		}

		list()
		{
    
    
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}

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

			 _head          tail  newnode
			//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 insert(iterator pos, const T& x)
		{
    
    
			Node* cur = pos._node;
			Node* prev = cur->_prev;

			Node* newnode = new Node(x);

			// prev newnode cur
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;

			return iterator(newnode);
		}

		void pop_back()
		{
    
    
			erase(--end());
		}

		void pop_front()
		{
    
    
			erase(begin());
		}

		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;

			return iterator(next);
		}

	private:
		Node* _head;
	};

	void test_list1()
	{
    
    
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);

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

		it = lt.begin();
		while (it != lt.end())
		{
    
    
			*it *= 2;
			++it;
		}
		cout << endl;

		for (auto e : lt)
		{
    
    
			cout << e << " ";
		}
		cout << endl;
	}

	struct Pos
	{
    
    
		int _a1;
		int _a2;

		Pos(int a1 = 0, int a2 = 0)
			:_a1(a1)
			, _a2(a2)
		{
    
    }
	};

	void test_list2()
	{
    
    
		int x = 10;
		int* p1 = &x;

		cout << *p1 << endl;

		Pos aa;
		Pos* p2 = &aa;
		p2->_a1;
		p2->_a2;

		list<Pos> lt;
		lt.push_back(Pos(10, 20));
		lt.push_back(Pos(10, 21));

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

			++it;
		}
		cout << endl;
	}

	void Func(const list<int>& l)
	{
    
    
		list<int>::const_iterator it = l.begin();
		while (it != l.end())
		{
    
    
			//*it = 10;

			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

	void test_list3()
	{
    
    
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);

		Func(lt);
	}

	void test_list4()
	{
    
    
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);

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

		it = lt.begin();
		while (it != lt.end())
		{
    
    
			*it *= 2;
			++it;
		}
		cout << endl;

		for (auto e : lt)
		{
    
    
			cout << e << " ";
		}
		cout << endl;

		lt.push_front(10);
		lt.push_front(20);
		lt.push_front(30);
		lt.push_front(40);

		lt.pop_back();
		lt.pop_back();

		for (auto e : lt)
		{
    
    
			cout << e << " ";
		}
		cout << endl;

		auto pos = find(lt.begin(), lt.end(), 4);
		if (pos != lt.end())
		{
    
    
			lt.insert(pos, 40);
			//lt.insert(pos, 30);
			*pos *= 100;
		}

		for (auto e : lt)
		{
    
    
			cout << e << " ";
		}
		cout << endl;
	}
}

6. list与vector的对比

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

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

7. 结尾

关于C++的list容器我们就学到这里,C++的几个容器都是非常重要的知识点,他们的使用方法非常简单方便,但最重要的是我们要理解底层原理并掌握他们各自的优缺点和不同点,以便以后学习和工作当中灵活使用。
最后,感谢各位大佬的耐心阅读和支持,觉得本篇文章写的不错的朋友可以三连关注支持一波,如果有什么问题或者本文有错误的地方大家可以私信我,也可以在评论区留言讨论,再次感谢各位。

猜你喜欢

转载自blog.csdn.net/qq_43188955/article/details/131067279