-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.h
More file actions
65 lines (50 loc) · 944 Bytes
/
Copy pathArray.h
File metadata and controls
65 lines (50 loc) · 944 Bytes
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
/** A basic dynamic array */
#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
template <typename T>
class Array{
T *arr;
int capacity;
void expand(){
T *old = arr;
capacity*=2;
arr = new T[capacity];
for(int i=0; i<size; ++i) arr[i] = old[i];
delete[] old;
}
public:
int size;
Array(): size(0), capacity(16){
arr = new T[16];
}
Array(int Capacity): size(0), capacity(Capacity){
arr = new T[Capacity];
}
Array(Array<T> &other){
delete[] arr;
arr = new T[other.capacity];
capacity = other.capacity;
size = other.size;
for(int i=0; i<other.size; ++i) arr[i] = other.arr[i];
}
void add(T x){
arr[size++] = x;
if(size == capacity) expand();
}
T& operator[](int index){
return arr[index];
}
T& get(int index){
return arr[index];
}
int operator()(){
return size;
}
~Array(){
//std::cout << "Deleting Array\n";
delete[] arr;
//std::cout << "Done with Array\n";
}
};
#endif