-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.cpp
103 lines (81 loc) · 1.95 KB
/
main.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
/*
* @FileName : iterator/main.cpp
* @CreateAt : 2022/5/12
* @Author : Inno Fang
* @Email : innofang@yeah.net
* @Description: Simple implementation of iterator pattern
*/
#include <iostream>
#include <vector>
template<typename T, typename C>
class Iterator {
public:
typedef typename std::vector<T>::iterator iter_type;
explicit Iterator(C *container) : _container(container) {
_iter = _container->_data.begin();
}
void first() {
_iter = _container->_data.begin();
}
void next() {
_iter++;
}
bool done() {
return _iter == _container->_data.end();
}
iter_type current() {
return _iter;
}
private:
C *_container;
iter_type _iter;
};
template<class T>
class Container {
friend class Iterator<T, Container>;
public:
void add(T t) {
_data.push_back(t);
}
Iterator<T, Container> *iterator() {
return new ::Iterator<T, Container>(this);
}
private:
std::vector<T> _data;
};
class Data {
public:
explicit Data(int val = 0) : _val(val) {}
void setValue(int val) {
_val = val;
}
int value() {
return _val;
}
private:
int _val;
};
int main() {
std::cout << "__________ iterator with int __________" << std::endl;
Container<int> cont1;
for (int i = 0; i < 10; ++i) {
cont1.add(i);
}
Iterator<int, Container<int>> *it1 = cont1.iterator();
for (it1->first(); !it1->done(); it1->next()) {
std::cout << *it1->current() << std::endl;
}
std::cout << "__________ iterator with Custom Class __________" << std::endl;
Container<Data> cont2;
Data a(100), b(200), c(300);
cont2.add(a);
cont2.add(b);
cont2.add(c);
Iterator<Data, Container<Data>> *it2 = cont2.iterator();
for (it2->first(); !it2->done(); it2->next()) {
std::cout << it2->current()->value() << std::endl;
}
delete it1;
delete it2;
return 0;
}