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;
}
相关推荐
神仙别闹4 分钟前
基于C#+MySQL实现(WinForm)企业设备使用信息管理系统
开发语言·mysql·c#
czhaii16 分钟前
PLC脉冲位置 单片机跟踪读取记录显示
开发语言·c#
alden_ygq42 分钟前
当java进程内存使用超过jvm设置大小会发生什么?
java·开发语言·jvm
蜗牛沐雨1 小时前
Rust 中的 `PartialEq` 和 `Eq`:深入解析与应用
开发语言·后端·rust
Python私教1 小时前
Rust快速入门:从零到实战指南
开发语言·后端·rust
Mcworld8571 小时前
整数分解JAVA
java·开发语言
请你喝好果汁6412 小时前
python_竞态条件
开发语言·python
正在走向自律2 小时前
Python 数据分析与可视化:开启数据洞察之旅(5/10)
开发语言·人工智能·python·数据挖掘·数据分析
吃个早饭2 小时前
2025年第十六届蓝桥杯大赛软件赛C/C++大学B组题解
c语言·c++·蓝桥杯
我来整一篇2 小时前
用Redis的List实现消息队列
数据库·redis·list