STL-List常用接口

List常用接口

insert

cpp 复制代码
list<int>::iterator pos = find(lt.begin(), lt.end(), 3);
if (pos != lt.end())
	lt.insert(pos, 30);
for (auto e : lt)
	cout << e << " ";
cout << endl;

list的不会失效,而vector会失效。

erase后均会失效。

解决迭代器失效问题

cpp 复制代码
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
	if (*it % 2 == 0)
		it = lt.erase(it);
	else
		it++;
}

核心 erase会返回以删除位置的下一个位置。

清空列表的两种方式

cpp 复制代码
while (it != lt.end())
{
	lt.erase(it++);
}
lt.clear();

模拟实现list

模拟node节点

cpp 复制代码
template<class T>
struct __list_node
{
	__list_node<T>* _next;
	__list_node<T>* _prev;
	T _data;
	__list_node(const T& x=T())
		:_data(x)
		,_next(nullptr)
		,_prev(nullptr)
	{}
};

模拟构造迭代器

cpp 复制代码
template<typename T>
struct __list_iterator
{
	typedef __list_node<T> Node;
	Node* _node;
	__list_iterator(Node* node)
		:_node(node)
	{}
	//*it;
	T& operator*()
	{
		return _node->_data;
	}
	//++it
	__list_iterator<T> operator++()
	{
		_node = _node->_next;
		return *this;
	}
	//it!=end()
	bool operator!=(__list_iterator<T>& it)
	{
		return _node != it->_node;

	}
};

按照STL原码模拟实现list的结构

cpp 复制代码
template<class T>
class list
{
	typedef struct __list_node<T> Node;
	typedef __list_iterator<T> iterator;
public:
	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);
		
		tail->_next = newnode;
		newnode->_prev = tail;
		_head->_prev = newnode;
		newnode->_next = _head;

	}
private:
	Node* _head;
};
cpp 复制代码
iterator begin()
{
	return iterator(_head->_next);
}
iterator end()
{
	return iterator(_head);
}

有了迭代器就可以使用范围for

cpp 复制代码
T* operator->()
{
	return &_node->_data;
}
cpp 复制代码
struct Date
{
	int _year = 0;
	int _mouth = 1;
	int _day = 1;
};
void test_list()
{
	list<Date> lt;
	lt.push_back(Date());
	lt.push_back(Date());
	list<Date>::iterator it = lt.begin();
	while (it != lt.end())
	{
		cout << it->_year << it->_mouth << it->_day << endl;
		++it;
	}
}
cpp 复制代码
while (it != lt.end())
{
	//cout << it->_year << it->_mouth << it->_day << endl;
	cout << (*it)._year << (*it)._mouth << (*it)._day << endl;
	++it;
}

前后置++

cpp 复制代码
//++it
__list_iterator<T>& operator++()
{
	_node = _node->_next;
	return *this;
}
__list_iterator<T>& operator++()
{
	_node = _node->_prev;
	return *this;
}
//it++	 
__list_iterator<T> operator++(int)
{
	__list_iterator<T> tmp(*this);
	_node = _node->_next;
	return tmp;
}
cpp 复制代码
//it++	 
__list_iterator<T> operator++(int)
{
	__list_iterator<T> tmp(*this);
	_node = _node->_next;
	//(*this)++;
	return tmp;
}
相关推荐
另寻沧海3 小时前
测试中的 AAA 模式与 Given–When–Then 模式详解
c++·单元测试·测试覆盖率
执尺量北斗4 小时前
[特殊字符] 基于 Qt + OpenGL 实现的入门级打砖块游戏
开发语言·qt·游戏
夏子曦4 小时前
C#内存管理深度解析:从栈堆原理到高性能编程实践
开发语言·c#
Q741_1474 小时前
C++ 模拟题 力扣495. 提莫攻击 题解 每日一题
c++·算法·leetcode·模拟
青岛少儿编程-王老师6 小时前
CCF编程能力等级认证GESP—C++7级—20250927
数据结构·c++·算法
Miki Makimura6 小时前
Reactor 模式实现:从 epoll 到高并发调试
运维·服务器·c++·学习
jiajixi6 小时前
Go 异步编程
开发语言·后端·golang
QX_hao6 小时前
【Go】--strings包
开发语言·后端·golang
计算机毕业设计木哥6 小时前
计算机毕设选题推荐:基于Hadoop和Python的游戏销售大数据可视化分析系统
大数据·开发语言·hadoop·python·信息可视化·spark·课程设计
秦禹辰7 小时前
venv与conda:Python虚拟环境深度解析助力构建稳定高效的开发工作流
开发语言·后端·golang