-
Notifications
You must be signed in to change notification settings - Fork 1
/
resizeArray.cpp
86 lines (60 loc) · 1.67 KB
/
resizeArray.cpp
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
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
// Problem 4 Part 2: Contiguous arrays using new/delete
template<typename Type>
class DArray{
private:
unsigned long limit;
unsigned long size;
Type* theData;
void resize(unsigned long newSize){
Type* newData = new Type[newSize];
for(unsigned long i = 0; i < size; i++){
newData[i] = theData[i];
}
delete[] theData;
theData = newData;
limit = size;
}
void addMoreData(Type inData){
if (size >= limit){
resize(2 * limit);
};
theData[size] = inData;
size++;
};
Type* getData(Type search){
for(unsigned long i = 0; i < size; i++){
if (theData[i] == search){
return &theData[i];
}
}
return 0;
};
public:
DArray(){
size = 0;
limit = 1;
theData = new Type[limit];
};
~DArray(){
delete[] theData;
};
void add(Type inData){
addMoreData(inData);
};
Type* getArray(){
return theData;
}
};
int main() {
DArray<char> myArray;
char theChar = 'a';
for(int i = 0; i < 26; i++){
myArray.add(theChar);
theChar++;
cout << myArray.getArray()[i] << endl;
}
}