This repository has been archived by the owner on Jul 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyvector.h
150 lines (123 loc) · 2.62 KB
/
myvector.h
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
#ifndef MYVECTOR_H
#define MYVECTOR_H
template <class T>
class MyVector {
private:
T* vect;
unsigned int Size;
unsigned int Capacity;
void resize();
public:
MyVector();
~MyVector();
T& operator[](unsigned int) const;
void Push(T);
void Pop(unsigned int = 0);
unsigned int GetSize() const;
unsigned int GetCapacity() const;
class Iterator {
private:
T* p;
public:
Iterator(T*);
T& operator*() const;
T* operator->() const;
Iterator& operator++();
Iterator& operator--();
bool operator==(const Iterator&) const;
bool operator!=(const Iterator&) const;
};
Iterator begin() const;
Iterator end() const;
};
template <class T>
MyVector<T>::MyVector() : vect(nullptr),
Size(0),
Capacity(0) {}
template <class T>
MyVector<T>::~MyVector() {
delete[] vect;
}
template <class T>
T& MyVector<T>::operator[](unsigned int i) const {
return vect[i];
}
template <class T>
unsigned int MyVector<T>::GetCapacity() const {
return Capacity;
}
template <class T>
unsigned int MyVector<T>::GetSize() const {
return Size;
}
template <class T>
void MyVector<T>::Pop(unsigned int i) {
if (Size != 0 && i < Size) {
Size--;
T aux = vect[i];
for (unsigned int j = i; j < Size; j++) {
vect[j] = vect[j + 1];
}
vect[Size] = nullptr;
delete aux;
}
}
template <class T>
void MyVector<T>::resize() {
if (Capacity == 0) {
Capacity = 1;
} else {
Capacity *= 2;
}
T* aux = vect;
vect = new T[Capacity];
for (unsigned int i = 0; i < Size; i++) {
vect[i] = aux[i];
}
delete[] aux;
}
template <class T>
void MyVector<T>::Push(T t) {
if (Capacity == Size) {
resize();
}
vect[Size] = t;
Size++;
}
template <class T>
MyVector<T>::Iterator::Iterator(T* ptr) : p(ptr) {}
template <class T>
T& MyVector<T>::Iterator::operator*() const {
return *p;
}
template <class T>
T* MyVector<T>::Iterator::operator->() const {
return p;
}
template <class T>
typename MyVector<T>::Iterator& MyVector<T>::Iterator::operator++() {
p++;
return *this;
}
template <class T>
typename MyVector<T>::Iterator& MyVector<T>::Iterator::operator--() {
p--;
return *this;
}
template <class T>
bool MyVector<T>::Iterator::operator==(const Iterator& it) const {
return p == it.p;
}
template <class T>
bool MyVector<T>::Iterator::operator!=(const Iterator& it) const {
return p != it.p;
}
template <class T>
typename MyVector<T>::Iterator MyVector<T>::begin() const {
return Iterator(vect);
}
template <class T>
typename MyVector<T>::Iterator MyVector<T>::end() const {
return Iterator(vect + Size);
}
#endif