-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterator.h
More file actions
91 lines (79 loc) · 2 KB
/
Iterator.h
File metadata and controls
91 lines (79 loc) · 2 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
#pragma once
#include <cstddef>
#include <iterator>
template<typename T>
class Iterator
{
public:
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = T*;
using reference = T&;
using iterator_category = std::random_access_iterator_tag;
public:
Iterator() : ptr{ nullptr } {}
Iterator(pointer ptr) : ptr{ ptr } {}
reference operator*() const { return *ptr; }
pointer operator->() const { return ptr; }
reference operator[](difference_type n) const { return *(ptr + n); }
Iterator<T>& operator++()
{
++ptr;
return *this;
}
Iterator<T> operator++(int)
{
Iterator<T> temp = *this;
++ptr;
return temp;
}
Iterator<T>& operator--()
{
--ptr;
return *this;
}
Iterator<T> operator--(int)
{
Iterator<T> temp = *this;
--ptr;
return temp;
}
Iterator<T>& operator+=(difference_type n)
{
ptr += n;
return *this;
}
Iterator<T>& operator-=(difference_type n)
{
ptr -= n;
return *this;
}
friend bool operator==(const Iterator<T>& lhs, const Iterator<T>& rhs) { return lhs.ptr == rhs.ptr; }
friend bool operator!=(const Iterator<T>& lhs, const Iterator<T>& rhs) { return !(lhs == rhs); }
friend bool operator<(const Iterator<T>& lhs, const Iterator<T>& rhs) { return lhs.ptr < rhs.ptr; }
friend bool operator>(const Iterator<T>& lhs, const Iterator<T>& rhs) { return rhs < lhs; }
friend bool operator<=(const Iterator<T>& lhs, const Iterator<T>& rhs) { return !(rhs < lhs); }
friend bool operator>=(const Iterator<T>& lhs, const Iterator<T>& rhs) { return !(lhs < rhs); }
friend Iterator<T> operator+(const Iterator<T>& it, difference_type n)
{
Iterator<T> temp = it;
temp += n;
return temp;
}
friend Iterator<T> operator+(difference_type n, const Iterator<T>& it)
{
return it + n;
}
friend Iterator<T> operator-(const Iterator<T>& it, difference_type n)
{
Iterator<T> temp = it;
temp -= n;
return temp;
}
friend difference_type operator-(const Iterator<T>& lhs, const Iterator<T>& rhs)
{
return lhs.ptr - rhs.ptr;
}
private:
pointer ptr;
};