forked from parallel101/hw02
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference.cpp
More file actions
74 lines (60 loc) · 1.67 KB
/
reference.cpp
File metadata and controls
74 lines (60 loc) · 1.67 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <vector>
template <typename T>
class MyRandomAccessIterator {
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = T*;
using reference = T&;
MyRandomAccessIterator(pointer ptr)
: m_ptr(ptr) {}
// 解引用运算符
reference operator*() const { return *m_ptr; }
pointer operator->() const { return m_ptr; }
// 前缀自增运算符
MyRandomAccessIterator& operator++() {
++ m_ptr;
return *this;
}
// 后缀自增运算符
MyRandomAccessIterator operator++(int) {
MyRandomAccessIterator tmp = *this;
++(*this);
return tmp;
}
// 随机访问运算符
reference operator[](size_t index) const { return *(m_ptr + index); }
// 赋值运算符
bool operator==(MyRandomAccessIterator const& other) const { return m_ptr == other.m_ptr; }
bool operator!=(MyRandomAccessIterator const& other) const { return m_ptr != other.m_ptr; }
private:
pointer m_ptr;
};
template <typename T>
class MyContainer {
public:
using iterator = MyRandomAccessIterator<T>;
void add(T const& val) {
m_data.push_back(val);
}
iterator begin() { return iterator(m_data.data());}
iterator end() { return iterator(m_data.data() + m_data.size());}
private:
std::vector<T> m_data;
};
int main() {
MyContainer<float> container;
container.add(1);
container.add(2);
container.add(3);
for(auto it = container.begin(); it != container.end(); it ++)
std::cout << *it << ' ';
std::cout << '\n';
// 这里在迭代器中定义的 [] 索引符号
auto it = container.begin();
std::cout << it[2] << '\n';
// std::cout << container[2] << '\n'; 非法
return 0;
}