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;
}
相关推荐
watson_pillow44 分钟前
mfc按钮点击事件没有触发,且程序卡死
c++·mfc
_Kayo_1 小时前
JS深拷贝 浅拷贝、CSS垂直水平居中
开发语言·前端·javascript
云天徽上1 小时前
【数据可视化-87】2023-2024年中国各省人口变化深度分析与可视化:Python + pyecharts打造炫酷暗黑主题大屏
开发语言·python·信息可视化·数据可视化·pyecharts
404未精通的狗1 小时前
(C++)继承全解析及运用
开发语言·c++
李永奉1 小时前
C语言—数组和指针练习题合集(二)
c语言·开发语言
xo198820111 小时前
鸿蒙Des 加密解密 C++版本
c++·华为·harmonyos
C4程序员2 小时前
北京JAVA基础面试30天打卡08
java·开发语言·面试
God-Hrh2 小时前
JVM运维
java·开发语言·jvm
weixin_448617052 小时前
疏老师-python训练营-Day43复习日
开发语言·python
MATLAB代码顾问2 小时前
MATLAB实现遗传算法求解路网路由问题
开发语言·算法·matlab