forked from MouCoder/cpp_Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_iterator
More file actions
58 lines (57 loc) · 1.35 KB
/
list_iterator
File metadata and controls
58 lines (57 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//list迭代器的实现
//Ref->T& ptr->T*
template<class T, class Ref, class Ptr>
struct __list_iterator
{
typedef __list_node<T> node;
typedef __list_iterator<T, Ref, Ptr> self;
node* _node;
//迭代器的作用不是创建节点而是指向原有节点,因此使用指针即可
__list_iterator(node* node)
:_node(node)
{}
//list迭代器中不用实现拷贝构造、赋值运算符重载、析构,因为迭代器的本质并不是创建节点,使用默认的进行值拷贝即可。
bool operator!=(const self& s) const //不需要进行修改,设计成const无论是const和非const都可以调用
{
return _node != s._node;
}
bool operator==(const self& s) const
{
return _node == s._node;
}
//重载->
Ptr operator->() const
{
return &_node->data;
}
Ref operator*() const
{
return _node->data;
}
//前置++
self operator++()
{
_node = _node->next;//直接返回++后的结果
return *this;
}
//后置++
self operator++(int) //参数只是为了和前置构成重载,并没有实际意义,因此只给类型就可以了
{
self tmp = *this;
_node = _node->next;
return tmp;
}
//前置--
self operator--()
{
_node = _node->next;
return *this;
}
//后置--
self operator--(int)
{
self tmp = *this;
_node = _node->next;
return tmp;
}
};