-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator_test.cpp
173 lines (147 loc) · 4.07 KB
/
iterator_test.cpp
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <iostream>
#include <initializer_list>
struct input_iterator_tag {};
struct output_iterator_tag {};
struct forward_iterator_tag : public input_iterator_tag {};
struct bidirectional_iterator_tag : public forward_iterator_tag {};
struct random_access_iterator_tag : public bidirectional_iterator_tag {};
//template<typename T>
//class Iterator {
// /* ... */
//};
template<typename Category, typename T, typename Distance = ptrdiff_t,
typename Pointer = T*, typename Reference = T&>
struct Iterator {
using iterator_category = Category;
using value_type = T;
using pointer = Pointer;
using reference = Reference;
using difference_type = Distance;
};
template<typename T>
class VectorIterator;
//容器的实现
template<typename T>
class Vector {
public:
using Iterator = VectorIterator<T>;
Vector(const std::initializer_list<T>& il) :
_data_start(new int[il.size()]), _data_end(_data_start + il.size()) {
std::copy(il.begin(), il.end(), _data_start);
}
~Vector() {
delete[] _data_start;
}
/* ... */
Iterator begin();
Iterator end();
private:
T* _data_start;
T* _data_end;
T* _storage_end;
};
//容器相关迭代器的实现
template<typename T>
class VectorIterator : public Iterator<random_access_iterator_tag, T> {
public:
using _Base = Iterator<random_access_iterator_tag, T>;
using iterator_category = typename _Base::iterator_category;
using value_type = typename _Base::value_type;
using pointer = typename _Base::pointer;
using reference = typename _Base::reference;
using difference_type = typename _Base::difference_type;
explicit VectorIterator(T* p) : _data_pointer(p) {}
/* ... */
pointer operator->() { return _data_pointer; }
reference operator*() { return *_data_pointer; }
VectorIterator& operator++() {
++_data_pointer;
return *this;
}
const VectorIterator operator++(int) {
VectorIterator tmp(*this);
++* this;
return tmp;
}
VectorIterator& operator+=(ptrdiff_t n) {
_data_pointer += n;
return *this;
}
bool operator==(const VectorIterator& rhs) const {
return this->_data_pointer == rhs._data_pointer;
}
bool operator!=(const VectorIterator& rhs) const {
return !operator==(rhs);
}
/* ... */
private:
T* _data_pointer;
};
//容器迭代器生成函数的实现
template<typename T>
typename Vector<T>::Iterator Vector<T>::begin() {
return VectorIterator<T>(this->_data_start);
}
template<typename T>
typename Vector<T>::Iterator Vector<T>::end() {
return VectorIterator<T>(this->_data_end);
}
//template<typename T>
//typename VectorIterator<T>::value_type
//accumulate(VectorIterator<T> beg, VectorIterator<T> end) {
// typename VectorIterator<T>::value_type res(0);
// for (; beg != end; ++beg)
// res += *beg;
// return res;
//}
//
//template<typename T>
//T accumulate(T *beg, T *end) {
// T res(0);
// for (; beg != end; ++beg)
// res += *beg;
// return res;
//}
//迭代器特性类实现
template<typename T>
struct Iterator_traits {
typedef typename T::value_type value_type;
typedef typename T::iterator_category iterator_category;
/* ... */
};
template<typename T>
struct Iterator_traits<T*> {
typedef T value_type;
typedef random_access_iterator_tag iterator_category;
/* ... */
};
//累加算法实现
template<typename Iterator>
typename Iterator_traits<Iterator>::value_type
accumulate(Iterator beg, Iterator end) {
typename Iterator_traits<Iterator>::value_type res(0);
for (; beg != end; ++beg)
res += *beg;
return res;
}
//迭代器步进算法实现
template<typename InputIterator, typename Dist>
void advance(InputIterator& iter, Dist n, input_iterator_tag) {
while (n--) iter++;
}
template<typename RandomAccessIterator, typename Dist>
void advance(RandomAccessIterator& iter, Dist n, random_access_iterator_tag) {
iter += n;
}
template<typename Iterator, typename Dist>
void advance(Iterator& iter, Dist n) {
using iter_category = typename Iterator_traits<Iterator>::iterator_category;
advance(iter, n, iter_category());
}
int main() {
Vector<int> ivec{ 1, 2, 4, 5, 66 };
for (auto iter = ivec.begin(); iter != ivec.end(); ++iter)
std::cout << *iter << ' ';
std::cout << std::endl;
return 0;
}