-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwork35.cpp
More file actions
92 lines (77 loc) · 1.76 KB
/
Copy pathwork35.cpp
File metadata and controls
92 lines (77 loc) · 1.76 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
using namespace std;
class CheckedPtr
{
public:
CheckedPtr(int *b, int *e) : beg(b), end(e), curr(b) {}
CheckedPtr &operator++(); // prefix ++
CheckedPtr &operator--(); // prefix --
CheckedPtr operator++(int); // postfix ++
CheckedPtr operator--(int); // postfix --
int *GetBeg();
int *GetEnd();
int *GetCurr();
private:
int *beg; // pointer to beginning of the array
int *end; // one past the end of the array
int *curr; // current position within the array
};
// 前缀自增:先移动指针,再返回引用
CheckedPtr &CheckedPtr::operator++()
{
if (curr < end)
++curr;
return *this;
}
// 前缀自减:先移动指针,再返回引用
CheckedPtr &CheckedPtr::operator--()
{
if (curr > beg)
--curr;
return *this;
}
// 后缀自增:保存当前位置,移动指针,返回保存的副本
CheckedPtr CheckedPtr::operator++(int)
{
CheckedPtr temp = *this;
if (curr < end)
++curr;
return temp;
}
// 后缀自减:保存当前位置,移动指针,返回保存的副本
CheckedPtr CheckedPtr::operator--(int)
{
CheckedPtr temp = *this;
if (curr > beg)
--curr;
return temp;
}
int *CheckedPtr::GetBeg()
{
return beg;
}
int *CheckedPtr::GetEnd()
{
return end;
}
int *CheckedPtr::GetCurr()
{
return curr;
}
int main()
{
int n;
cin >> n;
int *array = new int[n];
for (int i = 0; i < n; i++)
cin >> array[i];
CheckedPtr cp(array, array + n);
for (; cp.GetCurr() < cp.GetEnd(); cp++)
cout << *cp.GetCurr() << " ";
cout << endl;
for (--cp; cp.GetCurr() > cp.GetBeg(); cp--)
cout << *cp.GetCurr() << " ";
cout << *cp.GetCurr() << endl;
delete[] array;
return 0;
}