【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;
};
相关推荐
爱吃烤鸡翅的酸菜鱼22 分钟前
java(3)数组的定义与使用
java·开发语言·idea·intellij idea
kingbal37 分钟前
SpringBoot:解析excel
java·开发语言·excel
咩咩大主教39 分钟前
Linux下的简单TCP客户端和服务器
linux·服务器·c语言·开发语言·c++·tcp/ip·网络编程
远望樱花兔40 分钟前
【d44】【Java】【力扣】160.相交链表
java·开发语言·leetcode
Amo Xiang1 小时前
Python练习宝典:Day 1 - 选择题 - 基础知识
开发语言·python
软件开发技术深度爱好者1 小时前
Python青少年简明教程目录
开发语言·python
西贝爱学习1 小时前
python新手的五个练习题
开发语言·python
Leighteen1 小时前
如何在微服务的日志中记录每个接口URL、状态码和耗时信息?
java·开发语言
yyyyyyykk1 小时前
Java后端框架---Spring
java·开发语言·spring
玄【学生党】1 小时前
【c++】介绍
开发语言·c++