-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.h
61 lines (48 loc) · 1.17 KB
/
Vector.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
//
// Created by dyeaaaronjr on 11/13/2023.
//
#ifndef VECTOR_VECTOR_H
#define VECTOR_VECTOR_H
#include <stdexcept>
template <typename T>
class Vector {
T *array;
size_t capacity;
size_t length;
void resize_to_fit(size_t index) {
int newCapacity = capacity;
while (newCapacity <= index) {
newCapacity *= 2;
}
T* temp = new T[newCapacity];
memcpy(temp, array, sizeof(T) * capacity);
capacity = newCapacity;
delete[] array;
array = temp;
}
public:
Vector() : capacity(1), length(0), array(new int[1]){};
explicit Vector(size_t size)
: capacity(size), length(1), array(new int[size]){};
size_t size() const { return length; }
inline void push_back(T element) { insert(length, element); }
void insert(size_t index, T element) {
if (index >= capacity) {
resize_to_fit(index);
}
if (index >= length) {
length = index + 1;
} else {
length++;
}
array[index] = element;
}
T &operator[](size_t index) const {
if (index >= length) {
throw std::range_error("index out of range");
}
return array[index];
}
~Vector() { delete[] array; }
};
#endif // VECTOR_VECTOR_H