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;
}
相关推荐
大麦大麦22 分钟前
深入剖析 Sass:从基础到进阶的 CSS 预处理器应用指南
开发语言·前端·css·面试·rust·uni-app·sass
hhw1991121 小时前
c#面试题整理6
java·开发语言·c#
Icomi_1 小时前
【神经网络】0.深度学习基础:解锁深度学习,重塑未来的智能新引擎
c语言·c++·人工智能·python·深度学习·神经网络
蠟筆小新工程師1 小时前
Deepseek可以通过多种方式帮助CAD加速工作
开发语言·python·seepdeek
不知道取啥耶2 小时前
C++ 滑动窗口
数据结构·c++·算法·leetcode
天道有情战天下2 小时前
python flask
开发语言·python·flask
zephyr_zeng3 小时前
VsCode + EIDE + OpenOCD + STM32(野火DAP) 开发环境配置
c语言·c++·vscode·stm32·单片机·嵌入式硬件·编辑器
帅弟1503 小时前
Day4 C语言与画面显示练习
c语言·开发语言
qhs15733 小时前
Kotlin字符串操作在Android开发中的应用示例
android·开发语言·kotlin
Stack Overflow?Tan904 小时前
c++实现在同一台主机两个程序实现实时通信
开发语言·c++