-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.cpp
73 lines (70 loc) · 1.54 KB
/
vector.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
#ifndef VECTOR_CPP
#define VECTOR_CPP
template<typename T>
class Vector {
T* arr;
unsigned int arrSize;
unsigned int vecSize;
public:
Vector() {
arr = new T[1];
arrSize = 1;
vecSize = 0;
}
void push_back(T v) {
if(arrSize == vecSize) {
T* tmp = new T[2*arrSize];
for(int i = 0; i < arrSize; i++)
tmp[i] = arr[i];
delete[] arr;
arrSize *= 2;
arr = tmp;
}
arr[vecSize] = v;
vecSize++;
}
T& operator[](unsigned int index) {
return arr[index];
}
unsigned int size() const {
return vecSize;
}
bool empty() const {
return vecSize == 0;
}
class iterator {
T* curr;
public:
iterator(const iterator& other) {
this -> curr = other.curr;
}
iterator(T* pointer) {
curr = pointer;
}
T operator*() {
return *curr;
}
iterator& operator++() {
curr++;
return *this;
}
iterator operator++(int) {
T tmp = *this;
curr++;
return tmp;
}
bool operator!=(const iterator& other) {
return other.curr != this->curr;
}
bool operator==(const iterator& other) {
return other.curr == this->curr;
}
};
iterator begin() const {
return iterator(arr);
}
iterator end() const {
return iterator(arr+vecSize);
}
};
#endif