-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlast_work.hpp
More file actions
179 lines (132 loc) · 3.99 KB
/
Copy pathlast_work.hpp
File metadata and controls
179 lines (132 loc) · 3.99 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
class intvector{
public:
// constructor
intvector(){
m_size = 0;
// initially no elements, size is 0
m_capacity = 1;
p = new int[m_capacity];
// we dynamically allocate
// an initial memory area
// of size 1 (there is space
// to add 1 element before
// a reallocation is needed)
}
// like push_back in std::vector
// same algorithm as
// "Reallocating dynamically allocated contiguous memory"
// in section 11.5
void push_back(int n){
// TODO: implement this function
m_size += 1;
if (m_size >= m_capacity){
// double capacity
m_capacity = m_capacity * 2;
// copy to new memory
intvector(v);
// add the new element to the correct index (max index = size - 1)
p[m_size - 1] = n;
}
else{
// add the new element to the correct index (max index = size - 1)
p[m_size - 1] = n;
}
}
// like size() in std::vector
int size() const {
// TODO: implement this function
return m_size;
}
// like capacity() in std::vector
int capacity() const {
// TODO: implement this function
return m_capacity;
}
// return the element at index i
// (consider how it is used in the main below)
int at(int i) const {
// TODO: implement this function
int tmp;
tmp = p[i];
return tmp;
}
// destructor: we need to write our own destructor
// with a delete[] instruction that deallocates
// the dynamically allocated contiguous memory
// we would have a memory leak otherwise because
// the default destructor wouldn't do this
~intvector(){
delete[] p;
}
// copy constructor
// (see explanation in exercise below)
intvector(const intvector& v){
m_capacity = v.m_capacity;
m_size = v.m_size;
p = new int[m_capacity];
for(int i = 0; i < m_size; i++){
p[i] = v.p[i];
}
}
// assignment operator
// (see explanation in exercise below)
intvector& operator=(const intvector& v){
// first of all we check whether this is being called
// during a reflexive assignment
// that is something such as a = a;
// if yes we don't do anything
// otherwise we enter the if statement
if(this != &v){
delete[] p;
m_capacity = v.m_capacity;
m_size = v.m_size;
p = new int[m_capacity];
for(int i = 0; i < m_size; i++){
p[i] = v.p[i];
}
}
return *this;
}
private:
int* p;
int m_size;
int m_capacity;
};
int main(){
intvector iv1;
for(int i = 0; i < 9; i++){
iv1.push_back(i);
// TODO: add printing instructions showing how the size and the capacity are updated
std::cout << "added: " << i << " " << "size: " << iv1.size() << " " << "capacity: " << iv1.capacity() << std::endl;
// prints content
for (int i = 0; i < iv1.size(); i++){
std::cout << iv1.at(i) << std::endl;
}
}
std::cout << "printing iv1: " << std::endl;
for(int i = 0; i < iv1.size(); i++){
std::cout << iv1.at(i) << std::endl;
}
std::cout << std::endl;
// calling the copy constructor
intvector iv2(iv1);
// calling the assignment operator
intvector iv3 = iv1;
iv1.push_back(9);
std::cout << "printing iv1:" << std::endl;
for(int i = 0; i < iv1.size(); i++){
std::cout << iv1.at(i) << std::endl;
}
std::cout << std::endl;
std::cout << "printing iv2:" << std::endl;
for(int i = 0; i < iv2.size(); i++){
std::cout << iv2.at(i) << std::endl;
}
std::cout << std::endl;
std::cout << "printing iv3:" << std::endl;
for(int i = 0; i < iv3.size(); i++){
std::cout << iv3.at(i) << std::endl;
}
std::cout << std::endl;
}