自己实现iterator迭代器

#define BOOST_ASIO_DISABLE_STD_CHRONO
#include <iterator>
#include <iostream>

struct List_node_base {
	List_node_base* M_next;
	List_node_base* M_prev;
};

template <class T>
struct List_node : public List_node_base {
	T M_data;
};

struct List_iterator_base {
	typedef size_t size_type;
	typedef ptrdiff_t difference_type;
	typedef std::bidirectional_iterator_tag iterator_category;

	List_node_base* M_node;
	List_iterator_base(List_node_base* x) : M_node(x) {}
	List_iterator_base() {}

	void M_incr() { M_node == M_node->M_next; }
	void M_decr() { M_node == M_node->M_prev; }

	bool operator==(const List_iterator_base& x) const {
		return M_node == x.M_node;
	}

	bool operator!=(const List_iterator_base& x) const {
		return M_node != x.M_node;
	}
};

template<class T, class Ref, class Ptr>
struct List_iterator : public List_iterator_base {
	typedef List_iterator<T, T&, T*> iterator;
	typedef List_iterator<T, const T&, const T*> const_iterator;
	typedef List_iterator<T, Ref, Ptr> Self;

	typedef T value_type;
	typedef Ptr pointer;
	typedef Ref reference;
	typedef List_node<T> Node;

	List_iterator(Node* x) : List_iterator_base(x) {}
	List_iterator() {}
	List_iterator(const iterator& x) : List_iterator_base(x.M_node) {}

	reference operator*() const { return ((Node*)M_node)->M_data; }
	pointer operator->() const { return &(operator*()); }

	Self& operator++() {
		this->M->incr();
		return *this;
	}

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

	Self& operator--() {
		this->M_decr();
		return *this;
	}

	Self operator--(int) {
		Self tmp = *this;
		this->M_Decr();
		return tmp;
	}
};

int main()
{

	getchar();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值