-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc++ myvector
57 lines (47 loc) · 813 Bytes
/
c++ myvector
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
template< class T >
class myVector {
private:
T* start;
int cur_size;
int capacity;
public:
typedef T* iterator;
myVector(int size = 0) : cur_size(size), capacity(4) {
start = new T[capacity + 1];
};
void push_back(T &t) {
if (cur_size == capacity) {
resize();
}
start[cur_size] = t;
cur_size++;
}
void pop_back() {
cur_size--;
}
T back() {
return start[cur_size - 1];
}
void resize() {
int new_cap = 2 * capacity;
T* old_start = start;
start = new T[new_cap + 1];
for (int idx = 0; idx < cur_size; idx++) {
start[idx] = old_start[idx];
}
capacity = new_cap;
delete [] old_start;
}
iterator begin() {
return start;
}
iterator end() {
return &start[cur_size];
}
int capacity() {
return capacity;
}
int size() {
return cur_size;
}
}