forked from gustavogutierrezutp/is304
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.cpp
More file actions
150 lines (121 loc) · 2.81 KB
/
vector.cpp
File metadata and controls
150 lines (121 loc) · 2.81 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
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
#include <iostream>
using namespace std;
template <typename T> class Vector {
private:
// Stores the elements of the vector
T *storage;
// Current number of elements in the vector
unsigned int sz;
// Maximum number of elements that storage can hold
unsigned int capacity;
// Policy for resizing the vector
double policy;
public:
Vector() {
storage = new T[5];
sz = 0;
capacity = 5;
policy = 1.5;
}
Vector(unsigned int c, double p = 1.5) {
storage = new T[c];
sz = 0;
capacity = c;
policy = p;
}
Vector(const Vector<T> &other) {
sz = other.size();
capacity = other.capacity;
policy = other.policy;
storage = new T[capacity];
for (unsigned int i = 0; i < sz; i++) {
storage[i] = other.storage[i];
}
}
~Vector() { delete[] storage; }
void push_back(const T &elem) {
if (sz == capacity) {
resize();
}
storage[sz] = elem;
sz++;
}
void push_back(const Vector<T> &other) {
reserve(sz + other.size());
for (unsigned int i = 0; i < other.size(); i++) {
push_back(other.storage[i]);
}
}
void pop_back() { sz--; }
unsigned int size() const { return sz; }
private:
void resize() {
capacity *= policy;
T *new_storage = new T[capacity];
for (unsigned int i = 0; i < sz; i++) {
new_storage[i] = storage[i];
}
delete[] storage;
storage = new_storage;
}
void reserve(unsigned int new_capacity) {
if (new_capacity > capacity) {
capacity = new_capacity;
T *new_storage = new T[capacity];
for (unsigned int i = 0; i < sz; i++) {
new_storage[i] = storage[i];
}
delete[] storage;
storage = new_storage;
}
}
public:
void shrink_to_fit() {
if (sz < capacity) {
T *new_storage = new T[sz];
for (unsigned int i = 0; i < sz; i++) {
new_storage[i] = storage[i];
}
delete[] storage;
storage = new_storage;
capacity = sz;
}
}
T &operator[](unsigned int index) { return storage[index]; }
const T &operator[](unsigned int index) const { return storage[index]; }
T &at(unsigned int index) {
if (index >= sz) {
throw out_of_range("Index out of range");
}
return storage[index];
}
const T &at(unsigned int index) const {
if (index >= sz) {
throw out_of_range("Index out of range");
}
return storage[index];
}
bool empty() const { return sz == 0; }
};
void printVector(const Vector<int> &v) {
for (unsigned int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
}
class Point {
private:
double x, y;
public:
Point(double x = 0, double y = 0) : x(x), y(y) {}
};
int main() {
Vector<int> u;
for (int i = 0; i < 10; i++) {
u.push_back(i);
}
printVector(u);
u[3] = 100;
printVector(u);
return 0;
}