forked from czHappy/MiniSTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstl_list_iterator.h
127 lines (102 loc) · 3.12 KB
/
stl_list_iterator.h
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// stl_list_iterator.h
// mini-stl
//
// Created by cz-mac on 2021/12/21.
//
#pragma once
#include <cstddef> // ptrdiff_t
#include "stl_iterator.h"
#include "stl_list_node.h"
namespace MiniSTL {
template <class T>
struct __list_iterator {
// alias declarations
typedef __list_iterator<T> self;
typedef __list_node<T>* link_type; //链接指针
typedef bidirectional_iterator_tag iterator_category; //迭代器类型 双向
typedef T value_type; //值
typedef T* pointer ; //指针
typedef T& reference; //引用
typedef ptrdiff_t difference_type; //指针距离类型
// data member
link_type node; // 指向list节点 从这个节点迭代
// ctor
__list_iterator() {}
explicit __list_iterator(link_type x) : node(x) {}
// dtor(trivial)
// 判定迭代器是否指向同一个节点
bool operator == (const self &rhs) const noexcept { return node == rhs.node; }
bool operator != (const self &rhs) const noexcept { return node != rhs.node; }
// dererence
reference operator*() const { return node->data; }
// member access 实际上可以把iterator看成指针 需要重载->操作
pointer operator->() const { return &(operator*()); }
// increasement
self& operator ++ () {
node = node->next;
return *this;
}
// 用一个冗余参数来区分先加还是后加
self operator ++(int i) {
self temp = *this;
++(*this);
return temp;
}
// decreasement
self& operator -- () {
node = node->prev;
return *this;
}
self operator -- (int i) {
self temp = *this;
--(*this);
return temp;
}
};
template <class T>
struct __list_const_iterator {
// alias declarations
using iterator = __list_iterator<T>;
using self = __list_const_iterator<T>;
using link_type = __list_node<T> *;
using iterator_category = bidirectional_iterator_tag;
using value_type = T;
using pointer = const T *; //指针是一个常量指针 表示不能修改此迭代器所指向的内容
using reference = const T &;
using difference_type = ptrdiff_t;
// data member
link_type node; // raw pointer link to list_node
// ctor
__list_const_iterator() {}
explicit __list_const_iterator(link_type x) : node(x) {}
__list_const_iterator(const iterator &x) : node(x.node) {}
// dtor(trivial)
bool operator==(const self &rhs) const noexcept { return node == rhs.node; }
bool operator!=(const self &rhs) const noexcept { return node != rhs.node; }
// dererence
reference operator*() const { return node->data; }
// member access
pointer operator->() const { return &(operator*()); }
// increasement
self &operator++() {
node = node->next;
return *this;
}
self operator++(int i) {
self temp = *this;
++(*this);
return temp;
}
// decreasement
self &operator--() {
node = node->prev;
return *this;
}
self operator--(int i) {
self temp = *this;
--(*this);
return temp;
}
};
} // namespace MiniSTL