【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;
};

相关推荐

  1. C++】list模拟实现深入理解iterator

    2024-05-04 08:06:02       33 阅读
  2. C++】list模拟实现

    2024-05-04 08:06:02       22 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-04 08:06:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-04 08:06:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-05-04 08:06:02       82 阅读
  4. Python语言-面向对象

    2024-05-04 08:06:02       91 阅读

热门阅读

  1. OpenResty 安装及lua-resty-redis

    2024-05-04 08:06:02       34 阅读
  2. C#面:如何部署 ASP.NET 页面

    2024-05-04 08:06:02       30 阅读
  3. Spring框架中的ResourceUtils:资源工具类深度解析

    2024-05-04 08:06:02       28 阅读
  4. PyTorch深度学习——数据输入和预处理

    2024-05-04 08:06:02       31 阅读
  5. 第七十章 Apache (UNIX® Linux macOS) 的替代选项

    2024-05-04 08:06:02       32 阅读
  6. 安卓手机APP开发__媒体开发部分__分享声音的输入

    2024-05-04 08:06:02       37 阅读
  7. Vue3组合式API + TS项目中手写国际化插件

    2024-05-04 08:06:02       36 阅读
  8. YOLO的版本及进阶历史

    2024-05-04 08:06:02       32 阅读