//迭代器的基本结构
struct _Slist_iterator_base
{
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef forward_iterator_tag iterator_category;
_Slist_node_base* _M_node; //指向节点基本结构
_Slist_iterator_base(_Slist_node_base* __x) : _M_node(__x) {}
void _M_incr() { _M_node = _M_node->_M_next; }
bool operator==( const _Slist_iterator_base& __x) const {
return _M_node == __x._M_node;
}
bool operator!=( const _Slist_iterator_base& __x) const {
return _M_node != __x._M_node;
}
};
//单项链表迭代器结构
template <class _Tp, class _Ref, class _Ptr>
struct _Slist_iterator : public _Slist_iterator_base
{
typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
typedef _Slist_iterator<_Tp, _Ref, _Ptr> _Self;
typedef _Tp value_type;
typedef _Ptr pointer;
typedef _Ref reference;
typedef _Slist_node<_Tp> _Node;
_Slist_iterator(_Node* __x) : _Slist_iterator_base(__x) {}
_Slist_iterator() : _Slist_iterator_base(0) {}
_Slist_iterator( const iterator& __x) : _Slist_iterator_base(__x._M_node) {}
reference operator *() const { return ((_Node*) _M_node)->_M_data; }
#ifndef __SGI_STL_NO_ARROW_OPERATOR
pointer operator ->() const { return &( operator*()); }
#endif /* __SGI_STL_NO_ARROW_OPERATOR */
//operator++,没有实现operator--,因为这是slist迭代器是Forward Iterator
_Self& operator ++()
{
_M_incr();
return * this;
}
_Self operator ++(int )
{
_Self __tmp = * this;
_M_incr();
return __tmp;
}
};
__________________________________________________________________________________________________
slist的数据结构