【C++之list的应用及模拟实现】

C++之list的应用及模拟实现

前言:
前面篇章学习了C++对于vector的基本使用以及常用接口的认识,接下来继续学习,C++的list双向链表的应用以及模拟实现等知识。
/知识点汇总/

1、list的简单介绍

C++中的list是一个双向链表容器,它允许在序列中的任何位置进行快速的插入和删除操作。
list由多个节点组成,每个节点包含数据以及指向其前一个和后一个节点的指针。
这种结构使得list在插入和删除元素时具有优秀的性能,因为只需要调整相邻节点的指针,而不需要像数组或向量那样移动大量元素。
list的一些主要特点和用法
1.双向性
list中的元素是双向链接的,这意味着可以从前往后遍历,也可以从后往前遍历。
2.快速插入和删除
在list中的任何位置插入或删除元素都是常数时间的操作,因为只需要更新相邻节点的指针。
3.不支持随机访问
由于list是链表结构,它不支持像数组或向量那样的随机访问。访问特定元素需要从头或尾开始遍历。
4.迭代器
list提供迭代器来访问和操作元素。迭代器支持前向和后向迭代,但不支持随机访问。
5.成员函数
list提供了丰富的成员函数来操作元素,如push_back()、push_front()、pop_back()、pop_front()、insert()、erase()等。
6.内存分配:list的元素是分散存储的,每个元素都单独分配内存。这有时会导致内存碎片的问题,尤其是在频繁插入和删除元素的情况下。

2、list的应用

2.1、构造函数+push_back+迭代器遍历

void test_list1()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	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;

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

2.2、reverse逆置+sort排序+unique去重

void test_list2()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(3);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(5);
	lt.push_back(4);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

	//逆置
	lt.reverse();
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

	//排序 -- 默认升序
	lt.sort();
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;
	//排序 -- 降序(仿函数)
	lt.sort(greater<int>());
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

	//去重(前提条件是有序序列)
	lt.unique();
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;
}

2.3、splice粘接/转移

void test_list3()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(3);
	lt.push_back(2);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;
	//粘接/转移
	//把链表中某个位置的元素,转移到当前位置之前。
	lt.splice(lt.end(), lt, lt.begin());
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

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

	
	//lt1.splice(lt1.begin(), lt, lt.begin(), lt.end());
	//lt.splice(lt.begin(), lt1, lt1.begin(),lt1.end());
	lt.splice(lt.begin(), lt1, lt1.begin(), next(lt1.begin(), 2));
	//lt.splice(lt.begin(), lt1);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;
	lt1.push_back(6);
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
}

2.4、merge合并+remove_if条件删除函数+remove指定元素删除

bool single_digit(const int& value)
{
	return (value < 5);
}

void test_list4()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

	//合并
	list<int> lt2;
	lt2.push_back(6);
	lt2.push_back(7);
	lt2.push_back(8);
	lt2.merge(lt);
	for (auto e : lt2)
	{
		cout << e << " ";
	}
	cout << endl;

	//条件删除函数
	lt2.remove_if(single_digit);
	for (auto e : lt2)
	{
		cout << e << " ";
	}
	cout << endl;

	//指定元素删除
	lt2.remove(7);
	for (auto e : lt2)
	{
		cout << e << " ";
	}
	cout << endl;
}

2.5、排序 – 效率比较

//排序 -- 效率比较
void test_list5()
{
	srand(time(0));
	const int N = 1000000;

	list<int> lt1;

	vector<int> v;

	for (int i = 0; i < N; i++)
	{
		auto e = rand() + i;
		lt1.push_back(e);
		v.push_back(e);
	}

	int begin1 = clock();
	//排序
	sort(v.begin(), v.end());
	int end1 = clock();

	int begin2 = clock();
	//排序
	lt1.sort();
	int end2 = clock();

	printf("vector sort:%d\n", end1 - begin1);
	printf("list sort:%d\n", end2 - begin2);
}

//排序 -- 效率比较
void test_list6()
{
	srand(time(0));
	const int N = 1000000;

	list<int> lt1;
	list<int> lt2;

	for (int i = 0; i < N; i++)
	{
		auto e = rand() + i;
		lt1.push_back(e);
		lt2.push_back(e);
	}

	int begin1 = clock();
	//拷贝vector
	vector<int> v(lt2.begin(), lt2.end());
	//排序
	sort(v.begin(), v.end());
	//拷贝回覆盖lt2
	lt2.assign(v.begin(), v.end());
	int end1 = clock();

	int begin2 = clock();
	//排序
	lt1.sort();
	int end2 = clock();

	printf("list2 sort:%d\n", end1 - begin1);
	printf("list1 sort:%d\n", end2 - begin2);
}

3、list深度剖析及模拟实现

3.1、list的结构体构建

3.2、list的构造函数和析构函数

//empty_init初始化
void empty_init()
{
	_head = new Node;
	_head->_next = _head;
	_head->_prev = _head;
	_size = 0;
}
//构造函数
list()
{
	empty_init();
}
//需要析构,一般就需要自己写深拷贝
//不需要析构,一般就不需要自己写深拷贝
//析构函数
~list()
{
	clear();//只清楚数据,不包括头结点
	delete _head;
	_head = nullptr;
}

3.3、list的insert/empty/erase/clear/size接口函数

//插入
void insert(iterator pos, const T& val)
{
	Node* cur = pos._node;
	Node* newnode = new Node(val);
	Node* prev = cur->_prev;
	//就回到了,prev / cur / newnode的链接关系
	prev->_next = newnode;
	newnode->_prev = prev;
	newnode->_next = cur;
	cur->_prev = newnode;
	_size++;
}
//删除 --- 根据erase迭代器失效的知识点,需要返回下一个位置的迭代器(最优解)
iterator erase(iterator pos)
{
	Node* cur = pos._node;
	Node* prev = cur->_prev;
	Node* next = cur->_next;
	//prev / cur / next的链接关系
	prev->_next = next;
	next->_prev = prev;
	//释放当前节点
	delete cur;
	_size--;
	return iterator(next);
}
//判空
bool empty()
{
	return _size == 0;
}
//clear
void clear()
{
	iterator it = begin();
	while (it != end())
	{
		it = erase(it);
	}
}
//size
T size()
{
	return _size;
}

3.4、list的push_back/pop_back接口函数

//尾插 -- 复用
void push_back(const T& x)
{
	insert(end(), x);
}
//头插 -- 复用
void push_front(const T& x)
{
	insert(begin(), x);
}
//尾删
void pop_back()
{
	erase(--end());//注意参数是--end(),而不是end()-1,因为这里底层是对迭代器的操作。
}
//头删
void pop_front()
{
	erase(begin());
}

3.5、list的拷贝构造函数以及赋值构造函数

//交换
void swap(list<T>& lt)
{
	std::swap(_head, lt._head);
	std::swap(_size, lt._size);
}
//赋值拷贝
list<T>& operator=(list<int> lt)
{
	swap(lt);
	return *this;
}
//拷贝构造
list(const list<T>& lt)
{
	empty_init();
	for (auto& e : lt)
	{
		push_back(e);
	}
}

3.6、list的迭代器接口函数(const 权限问题)

typedef ListIterator<T> iterator;
typedef ListConstIterator<T> const_iterator;//只读
//begin()
iterator begin()
{
	//匿名构造
	//return iterator(_head->_next);
	//有名构造
	iterator it(_head->_next);
	return it;
	//单参数的构造函数可以使用隐式类型转换
	//return _head->_next;
}
//end()
iterator end()
{
	//return iterator(_head->_prev);error
	//注意end()是返回,最后一个数据的下一个位置
	return iterator(_head);
}
//函数重载 -- 解决const权限问题
//const 迭代器,需要的是迭代器不能修改,还是迭代器指向的内容不能修改?
//答:是迭代器指向的内容不能修改,所以返回值直接加const iterator是不行的。
//而是需要单独的类型名称,const_iterator
//T* const p1; --- 修饰指针
//const T* p2; --- 修饰指针指向的对象
//const_iterator --- 修饰指针
//const_iterator --- 修饰对象
//注意迭代器并不是指针,而是类型
const_iterator begin() const
{
	//单参数的构造函数可以使用隐式类型转换
	return _head->_next;
}
//end()
const_iterator end() const
{
	return _head;
}

3.7、list的迭代器接口函数(解决const权限问题)

const 迭代器,需要的是迭代器不能修改,还是迭代器指向的内容不能修改?

答:是迭代器指向的内容不能修改,所以返回值直接加const iterator是不行的,而是需要单独的类型名称,const_iterator

const_iterator begin() const
{
	//单参数的构造函数可以使用隐式类型转换
	return _head->_next;
}
//end()
const_iterator end() const
{
	return _head;
}

解决方法一
单独另写一个迭代器类的const 权限修饰
那么就得写一个const使用得,一个非const使用的iterator类。

//方法一:
//迭代器类的const 权限修饰
template<class T>
struct ListConstIterator
{
	typedef ListNode<T> Node;
	typedef ListConstIterator<T> Self;
	Node* _node;
	//构造函数
	ListConstIterator(Node* node)
		:_node(node)
	{}
	//运算符重载++ -- 前置 ++it
	Self& operator++()
	{
		_node = _node->_next;
		return *this;
	}
	//运算符重载++ -- 后置 it++
	Self operator++(int)
	{
		Self tmp(*this);//拷贝构造 -- 浅拷贝(迭代器不需要写深拷贝,因为本身迭代器都指向一块地址,同理,也不需要写析构,本身只需要访问,不需要管理。做一些释放等操作)
		_node = _node->_next;
		return tmp;
	}
	//运算符重载-- -- 前置 --it
	Self& operator--()
	{
		_node = _node->_prev;
		return *this;
	}
	//运算符重载-- -- 后置 it--
	Self operator--(int)
	{
		Self tmp(*this);//拷贝构造 -- 浅拷贝访问
		_node = _node->_prev;
		return tmp;
	}
	//解引用 -- *it(读/写 -- 传引用)
	const T& operator*()
	{
		return _node->_data;
	}
	//比较迭代器是否相等 -- 比较指针(地址)
	bool operator!= (const Self& it)
	{
		return _node != it._node;
	}
	//比较迭代器是否相等
	bool operator== (const Self& it)
	{
		return _node == it._node;
	}
	//运算符重载->
	const T* operator->()
	{
		return &_node->_data;
	}
};

解决方法二

利用模板类以及模板类多个参数,解决const 权限问题。多用于,只是类型不同,其他都相同的类。

//迭代器类的封装原生指针
//方法二:模板类
//本质相当于我们写了一个类模板,编译器实例化生成了两个类
//typedef ListIterator<T, T&, T*> iterator;
//typedef ListConstIterator<T, const T&, const T*> const_iterator;
template<class T, class Ref, class Ptr>
struct ListIterator
{
	typedef ListNode<T> Node;
	typedef ListIterator<T, Ref, Ptr> Self;//方法2,模板类
	Node* _node;
	//构造函数
	ListIterator(Node* node)
		:_node(node)
	{}
	//运算符重载++ -- 前置 ++it
	Self& operator++()
	{
		_node = _node->_next;
		return *this;
	}
	//运算符重载++ -- 后置 it++
	Self operator++(int)
	{
		Self tmp(*this);//拷贝构造 -- 浅拷贝(迭代器不需要写深拷贝,因为本身迭代器都指向一块地址,同理,也不需要写析构,本身只需要访问,不需要管理。做一些释放等操作)
		_node = _node->_next;
		return tmp;
	}
	//运算符重载-- -- 前置 --it
	Self& operator--()
	{
		_node = _node->_prev;
		return *this;
	}
	//运算符重载-- -- 后置 it--
	Self operator--(int)
	{
		Self tmp(*this);//拷贝构造 -- 浅拷贝访问
		_node = _node->_prev;
		return tmp;
	}
	//解引用 -- *it(读/写 -- 传引用)
	//T& operator*()
	Ref operator*()
	{
		return _node->_data;
	}
	//比较迭代器是否相等 -- 比较指针(地址)
	bool operator!= (const Self& it)
	{
		return _node != it._node;
	}
	//比较迭代器是否相等
	bool operator== (const Self& it)
	{
		return _node == it._node;
	}
	//运算符重载->
	//T* operator->()
	Ptr operator->()
	{
		return &_node->_data;
	}
};

对比两个方法:

typedef ListIterator<T> iterator;
//方法一:
typedef ListConstIterator<T> const_iterator;//只读

//方法二:模板类
typedef ListIterator<T, T&, T*> iterator;
//参考官方的做法:
//原型:typedef ListConstIterator<T, const T&, const T*> const_iterator;
typedef ListIterator<T, const T&, const T*> const_iterator;

采用方法二就只写一个irerator类了(类模板),只需要在list类里typedef使用就行了。

typedef ListIterator<T, T&, T*> iterator;
typedef ListIterator<T,const T&, const T*> const_iterator;

4、list模拟实现的完整代码

#define _CRT_SECURE_NO_WARNINGS 1

//list的剖析与模拟实现
#pragma once
#include <iostream>
#include <list>
#include <assert.h>

using namespace std;

namespace bit
{
	//公有 --- public
	template<class T>
	struct ListNode
	{
		T _data;
		ListNode<T>* _next;
		ListNode<T>* _prev;
		
		//构造函数
		ListNode(const T& x = T())
			:_data(x)
			,_next(nullptr)
			,_prev(nullptr)
		{}
	};

	//迭代器类的封装原生指针
	//方法二:模板类
	//本质相当于我们写了一个类模板,编译器实例化生成了两个类
	//typedef ListIterator<T, T&, T*> iterator;
	//typedef ListConstIterator<T, const T&, const T*> const_iterator;
	template<class T,class Ref,class Ptr>
	struct ListIterator
	{
		typedef ListNode<T> Node;
		typedef ListIterator<T, Ref, Ptr> Self;//方法2,模板类
		Node* _node;
		//构造函数
		ListIterator(Node* node)
			:_node(node)
		{}
		//运算符重载++ -- 前置 ++it
		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		//运算符重载++ -- 后置 it++
		Self operator++(int)
		{
			Self tmp(*this);//拷贝构造 -- 浅拷贝(迭代器不需要写深拷贝,因为本身迭代器都指向一块地址,同理,也不需要写析构,本身只需要访问,不需要管理。做一些释放等操作)
			_node = _node->_next;
			return tmp;
		}
		//运算符重载-- -- 前置 --it
		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		//运算符重载-- -- 后置 it--
		Self operator--(int)
		{
			Self tmp(*this);//拷贝构造 -- 浅拷贝访问
			_node = _node->_prev;
			return tmp;
		}
		//解引用 -- *it(读/写 -- 传引用)
		//T& operator*()
		Ref operator*()
		{
			return _node->_data;
		}
		//比较迭代器是否相等 -- 比较指针(地址)
		bool operator!= (const Self& it)
		{
			return _node != it._node;
		}
		//比较迭代器是否相等
		bool operator== (const Self& it)
		{
			return _node == it._node;
		}
		//运算符重载->
		//T* operator->()
		Ptr operator->()
		{
			return &_node->_data;
		}
	};

	//方法一:
	//迭代器类的const 权限修饰
	//template<class T>
	//struct ListConstIterator
	//{
	//	typedef ListNode<T> Node;
	//	typedef ListConstIterator<T> Self;
	//	Node* _node;
	//	//构造函数
	//	ListConstIterator(Node* node)
	//		:_node(node)
	//	{}
	//	//运算符重载++ -- 前置 ++it
	//	Self& operator++()
	//	{
	//		_node = _node->_next;
	//		return *this;
	//	}
	//	//运算符重载++ -- 后置 it++
	//	Self operator++(int)
	//	{
	//		Self tmp(*this);//拷贝构造 -- 浅拷贝(迭代器不需要写深拷贝,因为本身迭代器都指向一块地址,同理,也不需要写析构,本身只需要访问,不需要管理。做一些释放等操作)
	//		_node = _node->_next;
	//		return tmp;
	//	}
	//	//运算符重载-- -- 前置 --it
	//	Self& operator--()
	//	{
	//		_node = _node->_prev;
	//		return *this;
	//	}
	//	//运算符重载-- -- 后置 it--
	//	Self operator--(int)
	//	{
	//		Self tmp(*this);//拷贝构造 -- 浅拷贝访问
	//		_node = _node->_prev;
	//		return tmp;
	//	}
	//	//解引用 -- *it(读/写 -- 传引用)
	//	const T& operator*()
	//	{
	//		return _node->_data;
	//	}
	//	//比较迭代器是否相等 -- 比较指针(地址)
	//	bool operator!= (const Self& it)
	//	{
	//		return _node != it._node;
	//	}
	//	//比较迭代器是否相等
	//	bool operator== (const Self& it)
	//	{
	//		return _node == it._node;
	//	}
	//	//运算符重载->
	//	const T* operator->()
	//	{
	//		return &_node->_data;
	//	}
	//};


	template<class T>
	class list
	{
		typedef ListNode<T> Node;
	public:
		//typedef ListIterator<T> iterator;
		//方法一:
		//typedef ListConstIterator<T> const_iterator;//只读

		//方法二:模板类
		typedef ListIterator<T, T&, T*> iterator;
		//typedef ListConstIterator<T, const T&, const T*> const_iterator;
		typedef ListIterator<T, const T&, const T*> const_iterator;
		//empty_init初始化
		void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}
		//构造函数
		list()
		{
			empty_init();
		}
		//拷贝构造
		list(const list<T>& lt)
		{
			empty_init();
			for (auto& e : lt)
			{
				push_back(e);
			}
		}
		//需要析构,一般就需要自己写深拷贝
		//不需要析构,一般就不需要自己写深拷贝
		//析构函数
		~list()
		{
			clear();//只清楚数据,不包括头结点
			delete _head;
			_head = nullptr;
		}
		//clear
		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}
		//交换
		void swap(list<T>& lt)
		{
			std::swap(_head, lt._head);
			std::swap(_size, lt._size);
		}
		//赋值拷贝
		list<T>& operator=(list<int> lt)
		{
			swap(lt);
			return *this;
		}
		//尾插
		//void push_back(const T& x)
		//{
		//	Node* newnode = new Node(x);
		//	Node* tail = _head->_prev;
		//	tail->_next = newnode;
		//	newnode->_prev = tail;

		//	newnode->_next = _head;
		//	_head->_prev = newnode;
		//}
		//尾插 -- 复用
		void push_back(const T& x)
		{
			insert(end(), x);
		}
		//头插 -- 复用
		void push_front(const T& x)
		{
			insert(begin(), x);
		}
		//尾删
		void pop_back()
		{
			erase(--end());//注意参数是--end(),而不是end()-1,因为这里底层是对迭代器的操作。
		}
		//头删
		void pop_front()
		{
			erase(begin());
		}
		//插入
		void insert(iterator pos, const T& val)
		{
			Node* cur = pos._node;
			Node* newnode = new Node(val);
			Node* prev = cur->_prev;
			//就回到了,prev / cur / newnode的链接关系
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
			_size++;
		}
		//删除 --- 根据erase迭代器失效的知识点,需要返回下一个位置的迭代器(最优解)
		iterator erase(iterator pos)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;
			//prev / cur / next的链接关系
			prev->_next = next;
			next->_prev = prev;
			//释放当前节点
			delete cur;
			_size--;
			return iterator(next);
		}
		//begin()
		iterator begin()
		{
			//匿名构造
			//return iterator(_head->_next);
			//有名构造
			iterator it(_head->_next);
			return it;
			//单参数的构造函数可以使用隐式类型转换
			//return _head->_next;
		}
		//end()
		iterator end()
		{
			//return iterator(_head->_prev);error
			//注意end()是返回,最后一个数据的下一个位置
			return iterator(_head);
		}
		//函数重载 -- 解决const权限问题
		//const 迭代器,需要的是迭代器不能修改,还是迭代器指向的内容不能修改?
		//答:是迭代器指向的内容不能修改,所以返回值直接加const iterator是不行的。
		//而是需要单独的类型名称,const_iterator
		//T* const p1; --- 修饰指针
		//const T* p2; --- 修饰指针指向的对象
		//const_iterator --- 修饰指针
		//const_iterator --- 修饰对象
		//注意迭代器并不是指针,而是类型
		const_iterator begin() const
		{
			//单参数的构造函数可以使用隐式类型转换
			return _head->_next;
		}
		//end()
		const_iterator end() const
		{
			return _head;
		}
		//size
		T size()
		{
			return _size;
		}
		//判空
		bool empty()
		{
			return _size == 0;
		}
	private:
		Node* _head;
		size_t _size;
	};

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

		//实现遍历
		//迭代器的考虑 -- 内置类型的引用
		//1.内部类  2.typedef
		//本质是由于,原生指针不满足这里的应用,因为地址不连续
		//所以采用自定义类型重载运算符
		list<int>::iterator it = lt.begin();
		while (it != lt.end())
		{
			*it += 10;
			cout << *it << " ";
			it++;
		}
		cout << endl;

		lt.push_front(6);
		lt.push_front(7);
		lt.push_front(8);
		lt.push_front(9);
		lt.push_front(10);
		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;

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

		cout << lt.size() << endl;
		cout << lt.empty() << endl;
	}

	//特殊类型的链表的迭代器模拟访问以及迭代器多参数隐式类型转换
	struct A
	{
		int _a1;
		int _a2;
		A(int a1 = 0, int a2 = 0)
			:_a1(a1)
			,_a2(a2)
		{}
	};

	void test_list2()
	{
		list<A> lt;
		A aa1(1, 1);
		A aa2 = { 1,1 };//多参数的隐式类型转换

		lt.push_back(aa1);
		lt.push_back(aa2);
		lt.push_back(A(2,2));
		lt.push_back({3,3});
		lt.push_back({4,4});

		//迭代器模拟的是T*的行为
		list<A>::iterator it = lt.begin();
		while (it != lt.end())
		{
			//cout << (*it)._a1 << ":" << (*it)._a2 << endl;
			//运算符重载->
			cout << it->_a1 << ":" << it->_a2 << endl;
			//原型:it.operator->()->_a1
			it++;
		}
		cout << endl;
	}

	//const 链表不可修改问题,权限问题
	template<class T>
	void PrintList(const list<T>& clt)
	{
		//list<T>::const_iterator it = clt.begin(); --- error
		//解决方法2:typename+type --- 告诉编译器这是个类型。
		typename list<T>::const_iterator it = clt.begin();
		while (it != clt.end())
		{
			//*it += 10;//这样就不能被修改了,只读
			cout << *it << " ";
			it++;
		}
		cout << endl;
	}

	//const 迭代器,模板控制类型不同类
	void PrintList(const list<A>& clt)
	{
		list<A>::const_iterator it = clt.begin();
		while (it != clt.end())
		{
			cout << it->_a1 << ":" << it->_a2 << endl;
			it++;
		}
		cout << endl;
	}

	void test_list3()
	{
		list<A> lt;
		A aa1(1, 1);
		A aa2 = { 1,1 };//多参数的隐式类型转换

		lt.push_back(aa1);
		lt.push_back(aa2);
		lt.push_back(A(2, 2));
		lt.push_back({ 3,3 });
		lt.push_back({ 4,4 });
		//方法一:
		//函数重载,两个类
		//方法二:
		//最后用模板控制类型不同,封装这两个类
		PrintList(lt);

		list<int> lt2;
		lt2.push_back(1);
		lt2.push_back(2);
		lt2.push_back(3);
		lt2.push_back(4);
		lt2.push_back(5);

		PrintList(lt2);
	}

	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);
		PrintList(lt);

		//拷贝构造
		list<int> lt2(lt);
		PrintList(lt2);

		list<int> lt3;
		lt3 = lt2;
		PrintList(lt3);
	}
}

5、vector和list的优缺点比较

C++中的list和vector是两种常用的容器,它们各自具有不同的优缺点,适用于不同的场景。
下面是对list和vector的优缺点进行的总结

4.1、list的优缺点

优点

1.快速的插入和删除:在list中的任何位置插入或删除元素都是常数时间的操作,因为只需要更新相邻节点的指针,而不需要移动其他元素。
2.双向迭代:list支持双向迭代,可以从头到尾或从尾到头遍历元素。
3.内存分配灵活:list的元素是单独分配内存的,这可以避免大块内存分配带来的问题,特别是在处理大量小对象时。

缺点

1.不支持随机访问:list不支持像数组那样的随机访问元素,访问特定元素需要从头或尾开始遍历,这可能导致性能下降。
2.内存碎片:由于list的元素是分散存储的,频繁地插入和删除元素可能会导致内存碎片。
3.空间开销较大:list中的每个元素都需要额外的空间来存储指向前后节点的指针,这增加了空间开销。

4.2、vector的优缺点

优点

1.随机访问:vector支持快速随机访问元素,通过下标可以直接访问任意位置的元素。
2.内存连续:vector的元素在内存中是连续存储的,这有助于缓存优化,提高了访问效率。
3.尾部插入效率高:当在vector的尾部插入元素时,如果预留了足够的空间(通过reserve方法),则插入操作通常是非常高效的。

缺点

1.插入和删除可能效率低:在vector的中间位置插入或删除元素可能需要移动大量元素,这可能导致性能下降。
2.内存重新分配:当vector的大小达到其容量时,再添加元素会触发内存重新分配和元素复制或移动操作,这可能会带来性能开销。
3.空间开销:虽然vector本身不需要额外的空间来存储指针(如list),但如果预留了过多空间以避免重新分配,则可能会浪费内存。

4.3、总结

选择list还是vector取决于具体的应用场景和需求。

1.如果你需要频繁地在序列中间插入或删除元素,且不关心随机访问性能,那么list可能是一个更好的选择。
2.而如果你需要快速随机访问元素,并且在尾部添加元素的操作较多,那么vector可能更合适。
3.在某些情况下,它们各自具有不同的特性和适用场景。

  • 16
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值