【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;
};
相关推荐
aini_lovee4 分钟前
MATLAB基于小波技术的图像融合实现
开发语言·人工智能·matlab
R1nG86317 分钟前
多线程安全设计 CANN Runtime关键数据结构的锁优化
开发语言·cann
初次见面我叫泰隆18 分钟前
Qt——5、Qt系统相关
开发语言·qt·客户端开发
亓才孓23 分钟前
[Class的应用]获取类的信息
java·开发语言
初願致夕霞24 分钟前
Linux_进程
linux·c++
开开心心就好31 分钟前
AI人声伴奏分离工具,离线提取伴奏K歌用
java·linux·开发语言·网络·人工智能·电脑·blender
Never_Satisfied35 分钟前
在JavaScript / HTML中,关于querySelectorAll方法
开发语言·javascript·html
Thera7771 小时前
【Linux C++】彻底解决僵尸进程:waitpid(WNOHANG) 与 SA_NOCLDWAIT
linux·服务器·c++
3GPP仿真实验室1 小时前
【Matlab源码】6G候选波形:OFDM-IM 增强仿真平台 DM、CI
开发语言·matlab·ci/cd
Wei&Yan1 小时前
数据结构——顺序表(静/动态代码实现)
数据结构·c++·算法·visual studio code