-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_begin.cpp
193 lines (145 loc) · 4.95 KB
/
array_begin.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <cstddef>
#include <ios>
#include <iostream>
#include <iterator>
#include <type_traits>
#include <vector>
template <typename T, uint32_t N> // T = int
class array {
public:
using value_type = T;
using reference = T&; // int&
using const_reference = const T&;
using pointer = value_type *; // int *
using const_pointer = const value_type *;
using size_type = uint32_t;
array() = default;
array(const array&) = default;
array<T, N>& operator=(const array&) = default;
~array() = default;
pointer data() noexcept { return elements; } // returns int *
const_pointer data() const noexcept { return elements; } // returns const int *
reference front() { return elements[0]; }
const_reference front() const { return elements[0]; }
reference back() { return elements[N-1]; }
const_reference back() const { return elements[N-1]; }
reference operator[](size_type I) { return elements[I]; }
const_reference operator[](size_type I) const { return elements[I]; }
class array_iterator {
private:
pointer ptr;
public:
using value_type = T;
using pointer = T*;
using reference = T&;
using difference_type = std::ptrdiff_t;
using iterator_category = std::random_access_iterator_tag;
array_iterator() : ptr{nullptr} {}
array_iterator(pointer ptr_) : ptr{ptr_} {}
array_iterator(const array_iterator& iter) : ptr{iter.ptr} {}
array_iterator& operator=(const array_iterator &iter) {
ptr = iter.ptr;
return (*this);
}
reference operator*() { return *ptr; }
const_reference operator*() const { return *ptr; }
bool operator==(array_iterator other) {
return ptr == other.ptr;
}
bool operator!=(array_iterator other) {
return !(*this == other);
}
array_iterator& operator++() { // pre-increment
++ptr;
return (*this);
}
array_iterator& operator--() {
--ptr;
return (*this);
}
array_iterator operator++(int) { // post-increment
array_iterator temp = (*this);
++(*this);
return temp;
}
array_iterator operator--(int) {
array_iterator temp = (*this);
--(*this);
return temp;
}
array_iterator operator+(size_t offset) {
// return array_iterator(ptr + offset);
return { ptr + offset };
}
array_iterator operator-(size_t offset) {
return { ptr - offset };
}
std::ptrdiff_t operator-(array_iterator other) {
return ptr - other.ptr;
}
array_iterator& operator+=(size_t offset) {
ptr += offset;
return (*this);
}
array_iterator& operator-=(size_t offset) {
ptr -= offset;
return (*this);
}
};
using iterator = array_iterator;
iterator begin() { return iterator(elements); }
iterator end() { return iterator(elements + N); }
uint32_t size() const { return N; }
private:
T elements[N];
};
template <typename iter>
void swap(iter first, iter second) {
typename std::iterator_traits<iter>::value_type temp(*first);
*first = *second; // additional requirement: assignment
// operator on the component type (value_type)
*second = temp;
// iter temp = *first;
// *first = *second;
// *first = temp;
}
int main(int argc, char *argv[]) {
array<int, 4> A;
std::cout << A.size() << std::endl;
array<int, 4>::iterator begin, end, other;
begin = end;
A[0] = 45;
A[1] = 100;
A[2] = 200;
A[3] = 23;
// int a = 9, b = 7;
// int a = (9, 7);
std::cout << A[0] << std::endl;
std::cout << A.front() << std::endl;
A.front() = 10;
std::cout << A[0] << std::endl;
std::cout << std::is_same<array<int, 6>::value_type, int>::value << std::endl;
for (array<int, 4>::iterator first = std::begin(A),
last = std::end(A); first != last; ++first) {
std::cout << "[array]: " << *first << std::endl;
}
std::cout << std::boolalpha << std::is_same<
std::iterator_traits<array<int, 4>::iterator>::iterator_category,
std::bidirectional_iterator_tag>::value << std::endl;
std::cout << std::boolalpha << std::is_same<
std::iterator_traits<typename std::vector<int>::iterator>::iterator_category,
std::random_access_iterator_tag
>::value << std::endl;
std::vector<int> V{10, 20, 30, 40, 50 , 60};
// std::vector<int>::iterator iter_one = std::begin(V);
// std::vector<int>::iterator iter_five = iter_one + 4;
array<int, 4>::iterator i = std::begin(A);
array<int, 4>::iterator j = std::begin(A) + 3;
swap(i, j);
std::cout << *i << " " << *j << std::endl;
for (array<int, 4>::iterator first = std::begin(A),
last = std::end(A); first != last; ++first) {
std::cout << "[array]: " << *first << std::endl;
}
return 0;
}