【C++】List -- 详解

一、list的介绍及使用

https://cplusplus.com/reference/list/list/?kw=list

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

1、list的使用

list 中的接口比较多,此处类似,只需要掌握如何正确的使用,然后再去深入研究背后的原理,已达到可扩展的能力。以下为list中一些常见的重要接口。


(1)list的构造
cpp 复制代码
// list的构造
void TestList1()
{
    list<int> l1;                        // 构造空的l1
    list<int> l2(4, 100);                // l2中放4个值为100的元素
    list<int> l3(l2.begin(), l2.end());  // 用l2的[begin(), end())左闭右开的区间构造l3
    list<int> l4(l3);                    // 用l3拷贝构造l4

    // 以数组为迭代器区间构造l5
    int array[] = {16, 2, 77, 29};
    list<int> l5(array, array + sizeof(array) / sizeof(int));

    // 列表格式初始化C++11
    list<int> l6{1, 2, 3, 4, 5};

    // 用迭代器方式打印l5中的元素
    list<int>::iterator it = l5.begin();
    while (it != l5.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;

    // C++11范围for的方式遍历
    for (auto& e : l5)
    {
        cout << e << " ";
    }
    cout << endl;
}

(2)list iterator的使用

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

cpp 复制代码
// list迭代器的使用
void PrintList(const list<int>& l)
{
    // 注意这里调用的是list的 begin() const,返回list的const_iterator对象
    for (list<int>::const_iterator it = l.begin(); it != l.end(); ++it)
    {
        cout << *it << " ";
        // *it = 10; // 不能改变该值 -- 编译不通过
    }
    cout << endl;
}
cpp 复制代码
void TestList2()
{
    int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
    list<int> l(array, array + sizeof(array) / sizeof(array[0]));
    // 使用正向迭代器正向list中的元素
    // list<int>::iterator it = l.begin(); // C++98中语法
    auto it = l.begin();                   // C++11之后推荐写法
    while (it != l.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;

    // 使用反向迭代器逆向打印list中的元素
    // list<int>::reverse_iterator rit = l.rbegin();
    auto rit = l.rbegin();
    while (rit != l.rend())
    {
        cout << *rit << " ";
        ++rit;
    }
    cout << endl;
}

注意:遍历链表只能用迭代器范围for

【注意】

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

(3)list capacity

(4)list element access


(5)list modifiers
cpp 复制代码
// list插入和删除
// push_back/pop_back/push_front/pop_front
void TestList3()
{
    int array[] = {1, 2, 3};
    list<int> L(array, array + sizeof(array) / sizeof(array[0]));
    
    L.push_back(4); // 在list的尾部插入4
    L.push_front(0); // 在list的头部插入0
    PrintList(L);
    
    L.pop_back(); // 删除list尾部节点
    L.pop_front(); // 删除list头部节点
    PrintList(L);
}
cpp 复制代码
// insert /erase 
void TestList4()
{
    int array1[] = {1, 2, 3};
    list<int> L(array1, array1 + sizeof(array1) / sizeof(array1[0]));
   
    auto pos = ++L.begin(); // 获取链表中第二个节点
    cout << *pos << endl;
  
    L.insert(pos, 4); // 在pos前插入值为4的元素
    PrintList(L);
   
    L.insert(pos, 5, 5); // 在pos前插入5个值为5的元素
    PrintList(L);
   
    vector<int> v{7, 8, 9};
    L.insert(pos, v.begin(), v.end()); // 在pos前插入[v.begin(), v.end)区间中的元素
    PrintList(L);
   
    L.erase(pos); // 删除pos位置上的元素
    PrintList(L);
   
    L.erase(L.begin(), L.end()); // 删除list中[begin, end)区间中的元素,即删除list中的所有元素
    PrintList(L);
}
cpp 复制代码
// resize/swap/clear
void TestList5()
{
    // 用数组来构造list
    int array1[] = {1, 2, 3};
    list<int> l1(array1, array1 + sizeof(array1) / sizeof(array1[0]));
    PrintList(l1);
   
    list<int> l2;
    l1.swap(l2); // 交换l1和l2中的元素
    PrintList(l1);
    PrintList(l2);
  
    l2.clear(); // 将l2中的元素清空
    cout << l2.size() << endl;
}
为什么 C++98 建议使用各自容器里的 swap,而不建议使用算法里的 swap?

可以看到算法里 swap 的 C++98 的实现,无论是 string、vector、list 使用它会涉及深拷贝问题,而且这里的深拷贝代价极大,需要深拷贝 3 次 ------ 当 l1 和 l2 交换,这里会把 l1 拷贝构造一份 c,然后把 l2 赋值于 l1,c 赋值于 l2,完成交换。

而如果是容器里的 swap,需要交换 l1 和 l2,只需要头指针交换即可。假设是 vector,只要把 l1 和 l2 对应的 _start、_finish、_endofstorage 交换即可。相比算法里的 C++98 里的 swap,这里可以认为没有任何代价。


(6)list的迭代器失效

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

cpp 复制代码
void TestListIterator1()
{
    int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
    list<int> l(array, array+sizeof(array)/sizeof(array[0]));
    auto it = l.begin();
    while (it != l.end())
    {
        // erase()函数执行后,it所指向的节点已被删除,因此it无效,在下一次使用it时,必须先给其赋值
        l.erase(it); 
        ++it;
    }
}

// 改正
void TestListIterator()
{
    int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
    list<int> l(array, array+sizeof(array)/sizeof(array[0]));
    auto it = l.begin();
    while (it != l.end())
    {
        l.erase(it++); // it = l.erase(it);
    }
}

⚪【补充】

容器迭代器的分类:

  1. 使用功能的角度可分为,(正向、反向) + const
  2. 容器底层结构的角度可分为,单向、双向、随机

比如单链表迭代器、哈希表迭代器就是单向,特征是能 ++,不能 --;双向链表迭代器、map 迭代器就是双向,特征是能 ++、--;string、vector、deque 迭代器就是随机迭代器,特征是能 ++、--、+、-,一般随机迭代器底层都是一个连续的空间。


二、list的模拟实现

1、模拟实现list

要模拟实现 list,必须要熟悉 list 的底层结构以及其接口的含义。

cpp 复制代码
#pragma once

#include <iostream>
using namespace std;
#include <assert.h>

namespace xyl
{
	// List的节点类
	template<class T>
	struct ListNode
	{
		ListNode(const T& val = T())
			: _prev(nullptr)
			, _next(nullptr)
			, _val(val)
		{}

		ListNode<T>* _prev;
		ListNode<T>* _next;
		T _val;
	};

	template<class T, class Ref, class Ptr>
	class ListIterator
	{
		typedef ListNode<T> Node;
		typedef ListIterator<T, Ref, Ptr> Self;

		// Ref 和 Ptr 类型需要重定义下,实现反向迭代器的时候需要用到
	public:
		typedef Ref Ref;
		typedef Ptr Ptr;
	public:
		// 构造
		ListIterator(Node* node = nullptr)
			: _node(node)
		{}

		// 具有指针类似行为
		Ref operator*()
		{
			return _node->_val;
		}

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

		// 迭代器支持移动
		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}

		Self operator++(int)
		{
			Self temp(*this);
			_node = _node->_next;
			return temp;
		}

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

		Self operator--(int)
		{
			Self temp(*this);
			_node = _node->_prev;
			return temp;
		}

		// 迭代器支持比较
		bool operator!=(const Self& l)const
		{
			return _node != l._node;
		}

		bool operator==(const Self& l)const
		{
			return _node != l._node;
		}

		Node* _node;
	};

	template<class Iterator>
	class ReverseListIterator
	{
		// 注意:这里typename的作用是明确告诉编译器,Ref是Iterator类中的一个类型,而不是静态成员变量
		// 否则编译器编译时就不知道Ref是Iterator中的类型还是静态成员变量
		// 因为静态成员变量也是按照 类名::静态成员变量名 的方式访问的
	public:
		typedef typename Iterator::Ref Ref;
		typedef typename Iterator::Ptr Ptr;
		typedef ReverseListIterator<Iterator> Self;
	public:
		// 构造
		ReverseListIterator(Iterator it)
			: _it(it)
		{}

		// 具有指针类似行为
		Ref operator*()
		{
			Iterator temp(_it);
			--temp;
			return *temp;
		}

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

		// 迭代器支持移动
		Self& operator++()
		{
			--_it;
			return *this;
		}

		Self operator++(int)
		{
			Self temp(*this);
			--_it;
			return temp;
		}

		Self& operator--()
		{
			++_it;
			return *this;
		}

		Self operator--(int)
		{
			Self temp(*this);
			++_it;
			return temp;
		}

		// 迭代器支持比较
		bool operator!=(const Self& l)const
		{
			return _it != l._it;
		}

		bool operator==(const Self& l)const
		{
			return _it != l._it;
		}

		Iterator _it;
	};

	template<class T>
	class list
	{
		typedef ListNode<T> Node;

	public:
		// 正向迭代器
		typedef ListIterator<T, T&, T*> iterator;
		typedef ListIterator<T, const T&, const T&> const_iterator;

		// 反向迭代器
		typedef ReverseListIterator<iterator> reverse_iterator;
		typedef ReverseListIterator<const_iterator> const_reverse_iterator;
	public:
		// List的构造
		list()
		{
			CreateHead();
		}

		list(int n, const T& value = T())
		{
			CreateHead();
			for (int i = 0; i < n; ++i)
				push_back(value);
		}

		template <class Iterator>
		list(Iterator first, Iterator last)
		{
			CreateHead();
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

		list(const list<T>& l)
		{
			CreateHead();

			// 用l中的元素构造临时的temp,然后与当前对象交换
			list<T> temp(l.begin(), l.end());
			this->swap(temp);
		}

		list<T>& operator=(list<T> l)
		{
			this->swap(l);
			return *this;
		}

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		// List的迭代器
		iterator begin()
		{
			return iterator(_head->_next);
		}

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

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

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

		reverse_iterator rbegin()
		{
			return reverse_iterator(end());
		}

		reverse_iterator rend()
		{
			return reverse_iterator(begin());
		}

		const_reverse_iterator rbegin()const
		{
			return const_reverse_iterator(end());
		}

		const_reverse_iterator rend()const
		{
			return const_reverse_iterator(begin());
		}

		// List的容量相关
		size_t size()const
		{
			Node* cur = _head->_next;
			size_t count = 0;
			while (cur != _head)
			{
				count++;
				cur = cur->_next;
			}

			return count;
		}

		bool empty()const
		{
			return _head->_next == _head;
		}

		void resize(size_t newsize, const T& data = T())
		{
			size_t oldsize = size();
			if (newsize <= oldsize)
			{
				// 有效元素个数减少到newsize
				while (newsize < oldsize)
				{
					pop_back();
					oldsize--;
				}
			}
			else
			{
				while (oldsize < newsize)
				{
					push_back(data);
					oldsize++;
				}
			}
		}

		// List的元素访问操作
		// 注意:List不支持operator[]
		T& front()
		{
			return _head->_next->_val;
		}

		const T& front()const
		{
			return _head->_next->_val;
		}

		T& back()
		{
			return _head->_prev->_val;
		}

		const T& back()const
		{
			return _head->_prev->_val;
		}

		// List的插入
		void push_back(const T& val)
		{
			insert(end(), val);
		}

		// List的删除
		void pop_back()
		{
			erase(--end());
		}

		void push_front(const T& val)
		{
			insert(begin(), val);
		}

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

		// 在pos位置前插入值为val的节点
		iterator insert(iterator pos, const T& val)
		{
			Node* pNewNode = new Node(val);
			Node* pCur = pos._node;
			// 先将新节点插入
			pNewNode->_prev = pCur->_prev;
			pNewNode->_next = pCur;
			pNewNode->_prev->_next = pNewNode;
			pCur->_prev = pNewNode;
			return iterator(pNewNode);
		}

		// 删除pos位置的节点,返回该节点的下一个位置
		iterator erase(iterator pos)
		{
			// 找到待删除的节点
			Node* pDel = pos._node;
			Node* pRet = pDel->_next;

			// 将该节点从链表中拆下来并删除
			pDel->_prev->_next = pDel->_next;
			pDel->_next->_prev = pDel->_prev;
			delete pDel;

			return iterator(pRet);
		}

		void clear()
		{
			Node* cur = _head->_next;

			// 采用头删除删除
			while (cur != _head)
			{
				_head->_next = cur->_next;
				delete cur;
				cur = _head->_next;
			}

			_head->_next = _head->_prev = _head;
		}

		void swap(bite::list<T>& l)
		{
			std::swap(_head, l._head);
		}

	private:
		void CreateHead()
		{
			_head = new Node;
			_head->_prev = _head;
			_head->_next = _head;
		}
	private:
		Node* _head;
	};
}


// 对模拟实现的list进行测试
// 正向打印链表
template<class T>
void PrintList(const xyl::list<T>& l)
{
	auto it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
}

// 测试List的构造
void TestBiteList1()
{
	xyl::list<int> l1;
	xyl::list<int> l2(10, 5);
	PrintList(l2);

	int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
	xyl::list<int> l3(array, array + sizeof(array) / sizeof(array[0]));
	PrintList(l3);

	xyl::list<int> l4(l3);
	PrintList(l4);

	l1 = l4;
	PrintList(l1);
}

// PushBack()/PopBack()/PushFront()/PopFront()
void TestBiteList2()
{
	// 测试PushBack
	xyl::list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	PrintList(l);

	// 测试PopBack
	l.pop_back();
	l.pop_back();
	PrintList(l);

	l.pop_back();
	cout << l.size() << endl;

	// 测试PushFront
	l.push_front(1);
	l.push_front(2);
	l.push_front(3);
	PrintList(l);

	// 测试PopFront
	l.pop_front();
	l.pop_front();
	PrintList(l);

	l.pop_front();
	cout << l.size() << endl;
}

// 测试insert和erase
void TestBiteList3()
{
	int array[] = {1, 2, 3, 4, 5};
	xyl::list<int> l(array, array + sizeof(array) / sizeof(array[0]));

	auto pos = l.begin();
	l.insert(l.begin(), 0);
	PrintList(l);

	++pos;
	l.insert(pos, 2);
	PrintList(l);

	l.erase(l.begin());
	l.erase(pos);
	PrintList(l);

	// pos指向的节点已经被删除,pos迭代器失效
	cout << *pos << endl;

	auto it = l.begin();
	while (it != l.end())
	{
		it = l.erase(it);
	}
	cout << l.size() << endl;
}

// 测试反向迭代器
void TestBiteList4()
{
	int array[] = {1, 2, 3, 4, 5};
	xyl::list<int> l(array, array + sizeof(array) / sizeof(array[0]));

	auto rit = l.rbegin();
	while (rit != l.rend())
	{
		cout << *rit << " ";
		++rit;
	}
	cout << endl;

	const xyl::list<int> cl(l);
	auto crit = l.rbegin();
	while (crit != l.rend())
	{
		cout << *crit << " ";
		++crit;
	}
	cout << endl;
}

【List 的迭代器】
迭代器有两种实现方式,具体应根据容器底层数据结构实现:

  1. 原生态指针,比如:vector。
  2. 将原生态指针进行封装,因迭代器使用形式与指针完全相同。

因此在自定义的类中必须实现以下方法:

  1. 指针可以解引用 ,迭代器的类中必须重载 operator*()
  2. 指针可以通过 -> 访问其所指空间成员 ,迭代器类中必须重载 oprator->()
  3. 指针可以 ++ 向后移动 ,迭代器类中必须重载 operator++() 与 operator++(int)
  4. 至于operator--() / operator--(int) 释放需要重载,根据具体的结构来抉择,双向链表可以向前移动,所以需要重载,如果是 forward_list 就不需要重载 -- 。
  5. 迭代器需要进行是否相等的比较 ,因此还需要重载 operator==() 与 operator!=()

2、list的反向迭代器

通过前面我们可以知道,反向迭代器的 ++ 就是正向迭代器的 --,反向迭代器的 -- 就是正向迭代器的 ++,因此反向迭代器的实现可以借助正向迭代器,即: 反向迭代器内部可以包含一个正向迭代器,对正向迭代器的接口进行包装即可。

cpp 复制代码
template<class Iterator>
class ReverseListIterator
{
    // 注意:这里typename的作用是明确告诉编译器,Ref是Iterator类中的一个类型,而不是静态成员变量
    // 否则编译器编译时就不知道Ref是Iterator中的类型还是静态成员变量
    // 因为静态成员变量也是按照 类名::静态成员变量名 的方式访问的
public:
	typedef typename Iterator::Ref Ref;
    typedef typename Iterator::Ptr Ptr;
	typedef ReverseListIterator<Iterator> Self;
public:
	// 构造
	ReverseListIterator(Iterator it)
		: _it(it)
	{}

	// 具有指针类似行为
	Ref operator*()
	{
		Iterator temp(_it);
		--temp;
		return *temp;
	}

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

	// 迭代器支持移动
	Self& operator++()
	{
		--_it;
		return *this;
	}

	Self operator++(int)
	{
		Self temp(*this);
		--_it;
		return temp;
	}

	Self& operator--()
	{
		++_it;
		return *this;
	}

	Self operator--(int)
	{
		Self temp(*this);
		++_it;
		return temp;
	}

	// 迭代器支持比较
	bool operator!=(const Self& l)const
	{
		return _it != l._it;
	}

	bool operator==(const Self& l)const
	{
		return _it != l._it;
	}

	Iterator _it;
};

三、list与vector的对比

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

相关推荐
笃励17 分钟前
Java面试题二
java·开发语言·python
望森FPGA18 分钟前
HDLBits中文版,标准参考答案 |2.5 More Verilog Features | 更多Verilog 要点
学习·fpga开发
jyan_敬言26 分钟前
【Linux】Linux命令与操作详解(一)文件管理(文件命令)、用户与用户组管理(创建、删除用户/组)
linux·运维·服务器·c语言·开发语言·汇编·c++
l1x1n029 分钟前
DOS 命令学习笔记
笔记·学习·web安全
FL162386312930 分钟前
[C#]C# winform部署yolov11-pose姿态估计onnx模型
开发语言·yolo·c#
笑非不退41 分钟前
C++ 异步编程 并发编程技术
开发语言·c++
T0uken1 小时前
【QT Qucik】C++交互:接收QML信号
c++·qt·交互
道爷我悟了1 小时前
Vue入门-指令学习-v-on
javascript·vue.js·学习
爱写代码的刚子1 小时前
C++知识总结
java·开发语言·c++
martian6651 小时前
QT开发:基于Qt实现的交通信号灯模拟器:实现一个带有倒计时功能的图形界面应用
开发语言·qt