#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;
}
自己实现iterator迭代器
最新推荐文章于 2024-04-16 22:56:38 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)