-
Notifications
You must be signed in to change notification settings - Fork 0
/
VectorMemory_ResizeReserve.cpp
167 lines (136 loc) · 3.9 KB
/
VectorMemory_ResizeReserve.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
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
#include <iostream>
#include <vector>
using namespace std;
class Samp
{
private:
class Inner;
vector<Inner*> innerList;
class Inner
{
public:
int data;
vector<int> dataList;
Inner(int d)
{
data = d;
cout << "1. Data: " << data << "; Vec Size: " << dataList.size() << endl;
}
void printChild()
{
cout << "2. Data: " << data << "; Vec Size: " << dataList.size() << endl;
}
};
public:
void printParent();
void addInnerObj(int data);
~Samp()
{
cout << "Samp Destructor" << endl;
for (auto i : innerList)
{
i->printChild();
delete i;
}
}
};
void
Samp::printParent()
{
for (auto c : innerList)
{
cout << "In PrintParent Obj 2: " << c << endl;
c->printChild();
}
}
void
Samp::addInnerObj(int data)
{
Inner* inObj = new Inner(data);
innerList.push_back(inObj);
cout << "In addInnerObj Obj 1: " << inObj << endl;
}
// Bloomberg Phone Interview Questions
int main()
{
// Problem 1
{
cout << endl << "Problem 1" << endl;
vector<int> vec; // holds 0 elements
// The below line will crash the Program as we are using the memory without initializing
//int a = vec[0];
cout << "Cap: " << vec.capacity() << " ; Size: " << vec.size() << endl;
// Output: Cap: 0 ; Size: 0
}
// Problem 2: reserve
{
cout << endl << "Problem 2" << endl;
vector<int> vec; // holds 0 elements
// IMP: Doesn't ZERO the elements. Just allocates memory
vec.reserve(100); // Allocates space for 100 items, and vec2 IS STILL EMPTY.
// This works as we have allocated memory
int a = vec[0];
int b = vec[99];
cout << "Cap: " << vec.capacity() << " ; Size: " << vec.size() << endl;
cout << a << ", " << b << endl;
// Output: Cap: 100 ; Size: 0
// 6758944, 0
}
// Problem 3: resize
{
cout << endl << "Problem 3" << endl;
vector<int> vec; // holds 0 elements
vec.resize(100); // Allocates space for 100 items, and vec now has 100 items
// This works as we have allocated memory
int a = vec[0];
int b = vec[99];
cout << "Cap: " << vec.capacity() << " ; Size: " << vec.size() << endl;
cout << a << ", " << b << endl;
// Output: Cap: 100 ; Size: 100
// 0, 0
}
// Problem 4
{
cout << endl << "Problem 4" << endl;
vector<int> vec(100);
// This works as the memory is allocated and 0 gets printed
int a = vec[0];
int b = vec[99];
cout << "Cap: " << vec.capacity() << " ; Size: " << vec.size() << endl;
cout << a << ", " << b << endl;
// Output: Cap: 100 ; Size: 100
// 0, 0
}
// Problem 5
{
cout << endl << "Problem 5" << endl;
vector<int> vec(100);
int& a = vec[0]; // Creating a reference to vec[0]
a = 100; // Changing the reference should change vec[0]
cout << a << ", " << vec[0] << endl;
// Output: 100, 100
}
// Problem 6
// http://stackoverflow.com/questions/7181372/why-can-i-assign-a-new-value-to-a-reference-and-how-can-i-make-a-reference-refe
{
cout << endl << "Problem 6" << endl;
vector<int> vec(100);
int& a = vec[0];
int x = 200;
// HERE WE ARE JUST CHANGING THE VALUE THAT 'a' IS POINTING
// WE ARE NOT making 'a' point to x
a = x;
a = 500;
cout << a << ", " << vec[0] << ", " << x << endl;
// Output: 500, 500, 200
}
// Problem 7 Vector inside a class
{
cout << endl << "Problem 7" << endl;
Samp ob;
ob.addInnerObj(5);
ob.printParent();
}
cout << endl;
return 0;
}