list的iterator

template<class T, class Ref, class Ptr>
	class ListIterator
	{
		typedef ListNode<T>* PNode;
		typedef ListIterator<T, Ref, Ptr> Self;
	public:
		ListIterator(PNode pNode = nullptr)
			: _pNode(pNode)
		{}
		ListIterator(const Self& l)
			: _pNode(l._pNode)
		{}
		T& operator*()
		{
			return _pNode->_val;
		}
		T& operator->()
		{
			return &(operator*());
		}
		Self& operator++()
		{
			_pNode = _pNode->_pNext;
			return *this;
		}
		Self& operator++(int)
		{
			Self tmp(*this);
			_pNode = _pNode->_pNext;
			return *this;
		}
		bool operator==(const Self& l)
		{
			return l._pNode == _pNode;
		}
		bool operator!=(const Self& l)
		{
			return l._pNode != _pNode;
		}
		




		PNode _pNode;

	};
	//反向迭代器
	template<class T, class Ref, class Ptr, class Iterator>
	class ListReverseIterator
	{
		typedef ListReverseIterator<T, Ref, Ptr, Iterator> Self;
	public:
		ListReverseIterator(const Iterator& it)
			: _it(it)
		{}
		ListReverseIterator(const Self& s)
			: _it(s._it)
		{}
		Ref operator*()
		{
			Iterator tmp = _it;
			return *(--tmp);
		}
		Ptr operator->()
		{
			return &operator*();
		}
		//反向迭代器的++就是正向迭代器的--;相反,方向迭代器的--就是正向迭代器的++
		Self& operator++()
		{
			--_it;
			return *this; 
		}
		Self& operator++(int)
		{
			Iterator tmp(_it);
			--_it;
			return *this;
		}
		Self& operator--()
		{
			++_it;
			return *this;
		}
		Self& operator--(int)
		{
			Iterator tmp(_it);
			++_it;
			return *this;
		}
		bool operator!=(const Self& s)
		{
			return _it != s._it;
		}
		bool operator==(const Self& s)
		{
			return _it == s._it;
		}
	private:
		Iterator _it;
	};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值