【C++】从list模拟实现深入理解iterator

目录

前言

一、list节点类

二、迭代器类

三、list类


前言

在string与vector这类存储内存连续的容器中,iterator直接使用指针作为底层即可。但对于list这种存储内存不连续的容器,就不可能使用指针作为底层了。

本文我们将通过对list的学习及模拟实现深入了解iterator。


一、list节点类

先创建一个链表节点类。(注:C++中struct和class相似,struct中所有默认为public成员)

复制代码
//节点类
template<class T>
struct ListNode
{
	ListNode<T>* _prev;
	ListNode<T>* _next;
	T _val;

	ListNode(const T& value = T())
		: _prev(nullptr)
		, _next(nullptr)
		, _val(value)
	{}
};

二、迭代器类

为了方便像指针一样使用迭代器,需要对专门定义一个迭代器类,并对' * '、' ++ '、' -- '、' == '与' != '运算符进行重载。

复制代码
//迭代器类
template<class T, class Ref, class Ptr>
struct ListIterator
{
	typedef ListNode<T> Node;
	typedef ListIterator<T, Ref, Ptr> Self;

	ListIterator(Node* pNode = nullptr)
		:_pNode(pNode)
	{}

	ListIterator(const Self& l)
	{
		_pNode = l._pNode;
	}

	Ref operator*()
	{
		return _pNode->_val;
	}

	Ptr operator->()
	{
		return &_pNode->_val;
	}

	Self& operator++()
	{
		_pNode = _pNode->_next;
		return *this;
	}

	Self operator++(int)
	{
		Self tmp(*this);
		_pNode = _pNode->_next;
		return tmp;
	}

	Self& operator--()
	{
		_pNode = _pNode->_prev;
		return *this;
	}

	Self& operator--(int)
	{
		Self tmp(*this);
		_pNode = _pNode->_prev;
		return tmp;
	}

	bool operator!=(const Self& l)
	{
		return l._pNode != _pNode;
	}

	bool operator==(const Self& l)
	{
		return l._pNode == _pNode;
	}
	Node* _pNode;
	};

三、list类

完成了一、二两章的准备工作,我们很简单的就可以写出一个list类。

复制代码
template<class T>
class list
{
	typedef ListNode<T> Node;
	typedef ListIterator<T, T&, T*> iterator;
	typedef ListIterator<T, const T&, const T*> const_iterator;
public:
	///
	// List的构造
	list()
	{
		_head = new Node;
		_head->_next = _head;
		_head->_prev = _head;
		_size = 0;
	}

	list(int n, const T& value = T())
	{
		_head = new Node;
		_head->_next = _head;
		_head->_prev = _head;
		_size = 0;
		while (n--)
		{
			insert(end(), value);
		}
	}

	template <class Iterator>
	list(Iterator first, Iterator last)
	{
		_head = new Node;
		_head->_next = _head;
		_head->_prev = _head;
		_size = 0;
		while (first != last)
		{
			insert(end(), *first);
			++first;
		}
	}

	list(const list<T>& l)
	{
		_head = new Node;
		_head->_next = _head;
		_head->_prev = _head;
		_size = 0;
		for (auto e : l)
		{
			push_back(e);
		}
	}

	list<T>& operator=(const list<T> l)
	{
		swap(l);
		return *this;
	}

	~list()
	{
		clear();
		delete _head;
		_head = nullptr;
	}


	///
	// List Iterator
	iterator begin()
	{
		return _head->_next;
	}

	iterator end()
	{
		return _head;
	}

	const_iterator begin() const
	{
		return _head->_next;
	}

	const_iterator end() const
	{
		return _head;
	}


	///
	// List Capacity
	size_t size()const
	{
		return _size;
	}
	bool empty()const
	{
		return _size == 0;
	}


	
	// List Access
	T& front()
	{
		return begin()->_val;
	}

	const T& front()const
	{
		return begin()->_val;
	}

	T& back()
	{
		return end()->_prev->_val;
	}

	const T& back()const
	{
		return end()->_prev->_val;
	}


	
	// List Modify
	void push_back(const T& val) 
	{
		insert(end(), val); 
	}

	void pop_back() 
	{
		erase(--end());
	}

	void push_front(const T& val) 
	{
		insert(begin(), val); 
	}

	void pop_front() 
	{
		erase(begin()); 
	}

	// 在pos位置前插入值为val的节点
	iterator insert(iterator pos, const T& val)
	{
		Node* node = new Node(val);
		Node* cur = pos._pNode;
		Node* next = cur->_next;
		Node* prev = cur->_prev;
		node->_prev = prev;
		prev->_next = node;
		node->_next = cur;
		cur->_prev = node;
		++_size;
		return pos;
		}

	// 删除pos位置的节点,返回该节点的下一个位置
	iterator erase(iterator pos)
	{
		Node* cur = pos._pNode;
		Node* next = cur->_next;
		Node* prev = cur->_prev;
		delete cur;
		prev->_next = next;
		next->_prev = prev;
		--_size;
		return iterator(next);
	}

	void clear()
	{
		iterator it = begin();
		while (it != end())
		{
			it = erase(it);
		}
	}

	void swap(list<T>& l)
	{
		std::swap(_head, l._head);
		std::swap(_size, l._size);
	}

private:
	Node* _head;
	size_t _size;
};
相关推荐
淡海水3 小时前
07-04-并发-ConcurrentBag-T-工作窃取WorkStealing算法
开发语言·算法·c#·bag·concurrent·workstealing
CS_Zero4 小时前
C语言sizeof是函数吗?
c语言·开发语言
-今昭-4 小时前
《V2V 迁移实战:将 VMware 虚拟机转为 OpenStack 可用 Glance qcow2 镜像》
开发语言·python
黑马程序员毕设6 小时前
基于Java的医院药品管理系统的优化设计与实现
java·开发语言·spring boot·小程序·架构·课程设计·毕设
_Twink1e7 小时前
openJiuwen 实训营 Day 1 · WorkSwarm 入门教程
开发语言·c++·openjiuwen
问天_观心7 小时前
大模型微调学习(一)
开发语言·人工智能·学习·语言模型·github
阿里嘎多学长7 小时前
2026-09-03 GitHub 热点项目精选
开发语言·程序员·github·代码托管
dong_junshuai8 小时前
每天一个开源项目#87 fmt:24.5K Stars 的 C++ 格式化核心
c++·开源·github
源代码•宸8 小时前
前置准备:定时微服务背景和现状
开发语言·经验分享·后端·微服务·云原生·架构·golang
2601_967338718 小时前
C#上位机开发零基础入门到精通全套视频
开发语言·c#