注册 登录
编程论坛 C++论坛

求解~~~

weststreet 发布于 2011-04-29 09:26, 317 次点击
下面以List为例说明迭代器的原理
// List节点的定义
template<class T>
struct __list_node {
typedef void* void_pointer;
void_pointer next;
void_pointer prev;
T da
ta;
};
// List迭代器的定义
template<class T, class Ref, class Ptr>
struct __list_iterator {
// 这三个typedef是为了简化后面的代码书写
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 bidirectional_iterator_tag iterator_category;    // 迭代器类型属于bidirectional iterator
typedef T value_type;    // 值类型
typedef Ptr pointer;     // 指针类型
typedef Ref reference;   // 引用类型
typedef __list_node<T>* link_type;   // 节点指针类型
typedef size_t size_type;
typedef ptrdiff_t difference_type;
link_type node;  // 迭代器当前所指的节点


//下面三段如何理解?
typedef __list_iterator<T, T&, T*>            iterator;
typedef __list_iterator<T, const T&, const T*>const_iterator;
typedef __list_iterator<T, Ref, Ptr>          self;
0 回复
1