1
00:00:00,830 --> 00:00:03,590
Again, welcome to another lecture on structs.

2
00:00:04,010 --> 00:00:09,140
We're going to continue on talking about this subject, and we'll go into a little more detail about

3
00:00:09,140 --> 00:00:10,490
the different things we can do as strikes.

4
00:00:11,900 --> 00:00:16,370
So first thing I want to introduce is another way to initialize a struct.

5
00:00:16,940 --> 00:00:20,000
So you notice here we have a struct called employee.

6
00:00:20,030 --> 00:00:24,290
It has one two three four data members down here and main.

7
00:00:24,290 --> 00:00:29,780
When we make in an instance of an employee and we call that variable e one, you notice that we are

8
00:00:29,780 --> 00:00:36,080
able to initialize it kind of like in an array or vector initialization list form.

9
00:00:36,090 --> 00:00:40,640
So this is an initialization list like you can do with vectors and race and such.

10
00:00:41,060 --> 00:00:46,430
You notice that we're putting some values in here and you may be wondering, well, how do you know

11
00:00:46,430 --> 00:00:49,310
which one goes to which variable in our struct?

12
00:00:49,910 --> 00:00:51,590
It's actually just all in order.

13
00:00:52,630 --> 00:00:58,600
So since we have ID first, then name then group than ours, that's how it is from left to right here.

14
00:00:59,680 --> 00:01:03,790
So I.D. will be initialized to this number right here.

15
00:01:05,080 --> 00:01:08,290
Name will be initialized to Fred right here.

16
00:01:09,040 --> 00:01:14,970
Group will be initialized to see and ours will be initialized to this float.

17
00:01:14,980 --> 00:01:15,940
Forty three point five.

18
00:01:17,040 --> 00:01:22,830
So pretty cool, I just wanted to point out another way that you could initialize struts and of course,

19
00:01:22,830 --> 00:01:29,040
right here on this next line, we're just printing it out by using remember the dot operator here.

20
00:01:29,040 --> 00:01:33,060
So we're doing employ one I.D. to access the ID, which is this right?

21
00:01:33,060 --> 00:01:34,020
So this will print out.

22
00:01:34,440 --> 00:01:40,470
Then we do employ one name which will print this out a comma separated, separating all this by commas,

23
00:01:40,470 --> 00:01:43,530
uncrewed, which will be this right here.

24
00:01:43,530 --> 00:01:45,360
And then he went on, Ours right here.

25
00:01:45,390 --> 00:01:52,890
So the main thing is just to recognize that there is this other way to initialize your structs, the

26
00:01:52,890 --> 00:01:55,290
members of your structure, the data members.

27
00:01:57,390 --> 00:02:04,340
So the next thing I want to talk about is the fact that you can have arrays and vectors of struts.

28
00:02:04,350 --> 00:02:11,310
And what I mean by that with the of is that the data type of the array or the vector can be a struct

29
00:02:11,760 --> 00:02:14,780
type that you've defined, right, the abstract data type.

30
00:02:14,790 --> 00:02:17,600
So this is employee again, right?

31
00:02:18,390 --> 00:02:19,740
So let's go ahead and take a look.

32
00:02:21,470 --> 00:02:25,520
So right here we have our employees strut.

33
00:02:26,000 --> 00:02:29,530
We have the same thing ID, name group hours, right?

34
00:02:29,540 --> 00:02:36,260
Just like before you notice a main, what we do here is we create three employees with these initialization

35
00:02:36,260 --> 00:02:37,670
list styles, right?

36
00:02:37,850 --> 00:02:41,990
We're using that same thing where we have the brackets and separated by commas.

37
00:02:42,020 --> 00:02:46,860
It's a list and we're doing them in the order of the appearance.

38
00:02:46,880 --> 00:02:50,230
Top to bottom of the data members, right?

39
00:02:50,240 --> 00:02:52,410
So I.D. is the leftmost.

40
00:02:52,410 --> 00:02:55,150
The name comes here group is here, right?

41
00:02:55,160 --> 00:02:57,890
So same thing that we were just talking about in the previous slide.

42
00:02:59,420 --> 00:03:07,130
So below that, you notice we initialize this array of size three and the type of the array is employee.

43
00:03:08,830 --> 00:03:11,170
So let's take a look at that and let's see what's going on here.

44
00:03:11,740 --> 00:03:13,930
So here is that enhanced a bit in size.

45
00:03:14,440 --> 00:03:21,890
So we make an array of size three call it array for the variable air and type employee.

46
00:03:21,910 --> 00:03:27,700
So just like you put tape and we now have our own type right and abstract data type called employee,

47
00:03:27,700 --> 00:03:29,080
and that is the type of the array.

48
00:03:29,620 --> 00:03:30,790
Same thing for the vector.

49
00:03:30,790 --> 00:03:34,510
Normally here you could see end or string in between these, right?

50
00:03:35,320 --> 00:03:39,480
But now we're seeing our data type that we created employee, right?

51
00:03:39,490 --> 00:03:42,760
So same thing as putting an end or string or float in here.

52
00:03:42,790 --> 00:03:46,210
We're putting our own abstract data type inside of here.

53
00:03:47,080 --> 00:03:49,450
We call this effect right now.

54
00:03:49,450 --> 00:03:55,180
What I'm doing is I'm just setting each one of the positions of the array to be each one of the employees.

55
00:03:55,180 --> 00:04:04,120
So I say, Ah, which is our employee or a zero equals e one position, one equals E two position two

56
00:04:04,120 --> 00:04:05,200
equals E three, right?

57
00:04:05,200 --> 00:04:11,320
So we're taking these three employees we created and putting them in the three positions of our array

58
00:04:11,320 --> 00:04:11,710
of size.

59
00:04:11,710 --> 00:04:12,040
Three.

60
00:04:12,070 --> 00:04:12,340
Right.

61
00:04:13,270 --> 00:04:19,030
The vector, I'm using the pushback function and I'm just pushing back employees just like we kind of

62
00:04:19,030 --> 00:04:19,910
added them to the array.

63
00:04:19,930 --> 00:04:23,530
Now I'm using the push back function to add them to the employee vector.

64
00:04:25,380 --> 00:04:32,070
So then we come down to here where I'm actually printing them out, so I'm only printing out the array

65
00:04:32,070 --> 00:04:36,750
ones here, but you see when I commented, my comments over here show what it would be if it was the

66
00:04:36,750 --> 00:04:38,880
vector pretty much be the same thing, right?

67
00:04:39,360 --> 00:04:42,150
So all we're doing is indexing it just like normal.

68
00:04:42,150 --> 00:04:49,410
And then what we're doing, though, is putting a dot right here and then printing out each one of the

69
00:04:49,830 --> 00:04:50,600
attributes.

70
00:04:50,640 --> 00:04:53,820
One of the member sorry data members here, right?

71
00:04:53,820 --> 00:04:55,830
So ID name group hours.

72
00:04:56,160 --> 00:04:58,620
Remember, we access those with a dot.

73
00:04:58,830 --> 00:05:00,630
You might be wondering, Well, this is weird.

74
00:05:00,640 --> 00:05:03,630
You have the name of the array in your indexing.

75
00:05:03,840 --> 00:05:08,620
And then like right after the subscript operator brackets, here you put a dot.

76
00:05:08,640 --> 00:05:10,740
Isn't that a syntax problem and error?

77
00:05:11,340 --> 00:05:12,180
Well, no, it's not.

78
00:05:12,180 --> 00:05:15,100
Because let's think about what this is that I'm circling right here.

79
00:05:15,120 --> 00:05:21,540
What is e RR subscript i or index AI, right?

80
00:05:21,570 --> 00:05:23,910
Let's think about the first time that this loop runs.

81
00:05:23,910 --> 00:05:24,810
It's zero, right?

82
00:05:26,460 --> 00:05:32,580
So r i, what would that alone give us just this alone?

83
00:05:33,390 --> 00:05:40,620
Well, we noticed that e r zero is e one, which is an employee, right?

84
00:05:40,630 --> 00:05:42,180
We describe that employee here.

85
00:05:42,930 --> 00:05:46,580
So you can actually replace this whole thing that I'm circling right here.

86
00:05:47,400 --> 00:05:55,200
The E.R. subscript AI, when it's zero, you can replace that with A1 because that's what it's getting

87
00:05:55,200 --> 00:05:56,010
out of it, right?

88
00:05:56,040 --> 00:05:57,510
This was what's stored there.

89
00:05:57,510 --> 00:06:00,090
So it returns that back to us.

90
00:06:01,410 --> 00:06:07,840
Now, imagine doing a one I.D. that's essentially what's happening.

91
00:06:08,010 --> 00:06:12,360
We're indexing the array to get E1 out of positions zero right.

92
00:06:12,360 --> 00:06:14,770
When we do, the first loop is zero.

93
00:06:15,570 --> 00:06:18,750
So we're getting E one out right here.

94
00:06:19,200 --> 00:06:22,070
And then once we have one, we do dot I.D..

95
00:06:22,860 --> 00:06:26,050
So that is what C++ is seen here.

96
00:06:26,070 --> 00:06:28,930
You know, it's like, OK, this gets resolved it.

97
00:06:29,180 --> 00:06:31,170
Let's get whatever's out of that array.

98
00:06:31,200 --> 00:06:34,020
You know, all the bytes and that happens to be this object here.

99
00:06:35,100 --> 00:06:37,710
This sorry, the struct, which is E1.

100
00:06:38,220 --> 00:06:39,720
And then we do e wonder ID.

101
00:06:39,990 --> 00:06:41,460
And then I'm going to pronounce the ID.

102
00:06:41,640 --> 00:06:43,400
And then the vector would be the same thing.

103
00:06:43,410 --> 00:06:49,890
It's just event set of script I.D. instead of e a R because we just called it different, you know,

104
00:06:49,890 --> 00:06:51,810
but vectors are subscript at the same.

105
00:06:52,380 --> 00:06:59,910
The point is that when we use these subscript operator, which is these brackets here and we're indexing

106
00:06:59,910 --> 00:07:03,880
it, it's giving us this struct, employee type, right?

107
00:07:03,880 --> 00:07:07,620
And it's specifically for the first loop iteration is giving us E1.

108
00:07:07,860 --> 00:07:15,810
So each time we call ID Dot Name Dot Group to ours, we're really doing E1 to ID, E1 got name, E1

109
00:07:15,810 --> 00:07:17,730
Dot Group and E1 hours.

110
00:07:17,760 --> 00:07:18,090
All right.

111
00:07:19,690 --> 00:07:21,040
So that is what's going on.

112
00:07:22,300 --> 00:07:29,350
So now that we've kind of cleared that up and explained how to make vectors and arrays of strikes,

113
00:07:30,070 --> 00:07:32,280
let's go ahead and have a quick challenge.

114
00:07:32,290 --> 00:07:37,210
So I want you to pause the video after I explain this.

115
00:07:37,450 --> 00:07:44,560
And what you're going to do is read in data from a file into a vector of structures.

116
00:07:44,860 --> 00:07:46,800
The file is going to look like this.

117
00:07:46,810 --> 00:07:51,310
It's going to be here's like one line and here's another line and so on and so forth.

118
00:07:51,310 --> 00:07:58,810
It's going to be name, which is a string space age, which should be an integer space weight, which

119
00:07:58,810 --> 00:08:01,630
will be a float and there'll be multiple lines of that, right?

120
00:08:01,780 --> 00:08:05,620
So I want you to actually go ahead and create your own file that is in this format, right?

121
00:08:05,830 --> 00:08:07,810
It's like a string for a name.

122
00:08:07,810 --> 00:08:13,840
And then, you know, the age is a number and the way is some decimal number, decimal point number,

123
00:08:15,040 --> 00:08:19,780
and you're going to read that this stuff into a string and end in a float, respectively, right?

124
00:08:20,650 --> 00:08:28,930
So what I want you to do is like each line is going to be represented with a struct, an instance of

125
00:08:28,930 --> 00:08:29,540
a struct, right?

126
00:08:29,560 --> 00:08:32,890
We're talking about a variable think about like E1, right?

127
00:08:33,340 --> 00:08:43,390
So if this was an employee rather than like a person or whatever, you know, this would be this whole

128
00:08:43,390 --> 00:08:45,070
line here would be like E1, right?

129
00:08:45,130 --> 00:08:48,570
This would be to like if we're thinking back to this example, right?

130
00:08:48,580 --> 00:08:52,540
We had we made our struct here where we kind of defined it right.

131
00:08:52,570 --> 00:08:55,030
Then here we make an instance of our struct.

132
00:08:55,030 --> 00:08:57,940
We made a variable called E1, its type employee, right?

133
00:08:57,940 --> 00:08:59,710
And here is an instance of our struct.

134
00:09:00,370 --> 00:09:04,660
So each line in the file represents an instance of the struct.

135
00:09:04,670 --> 00:09:07,330
So I want you to be able to look to the file.

136
00:09:07,990 --> 00:09:14,980
Read all these in to a struct, and then I want you to make a vector of all these drugs and then print

137
00:09:14,980 --> 00:09:16,360
out that vector abstracts.

138
00:09:16,360 --> 00:09:18,490
OK, so that is your challenge.

139
00:09:19,510 --> 00:09:25,360
So you can go ahead and refer to the previous slides if you go back in the video if you need to go ahead

140
00:09:25,360 --> 00:09:26,380
and pause video here.

141
00:09:27,460 --> 00:09:30,220
All right, so let's go ahead and get started with this problem.

142
00:09:30,880 --> 00:09:34,990
So go ahead and go to the GPS code here.

143
00:09:35,230 --> 00:09:37,760
So I have a couple of files here.

144
00:09:37,780 --> 00:09:43,000
The first one is to keep I know I'm going to want to include I stream to print out.

145
00:09:43,000 --> 00:09:46,840
I'm going to include Airstream to read from a file and I'm going to include Vector because I'm going

146
00:09:46,840 --> 00:09:47,920
to have a vector of straps.

147
00:09:47,920 --> 00:09:49,570
Right here is the other file.

148
00:09:49,570 --> 00:09:50,800
It's a text file.

149
00:09:50,800 --> 00:09:53,560
It has each line as described in the problem.

150
00:09:53,800 --> 00:09:55,180
We have a name, right?

151
00:09:55,180 --> 00:10:01,030
Like Bob, he's age 20 and this float here is the weight, and he's two hundred two hundred point five

152
00:10:01,030 --> 00:10:03,250
pounds or kilograms where you want to call that.

153
00:10:04,120 --> 00:10:07,780
So let's go ahead and get going with this.

154
00:10:07,780 --> 00:10:10,740
So I'm going to know I'm going to make a struct for sure, right?

155
00:10:10,750 --> 00:10:16,720
So I'm going to say struct person and I'm going to go ahead and put a semicolon at the end because we

156
00:10:16,720 --> 00:10:17,490
know we need that.

157
00:10:17,500 --> 00:10:18,940
And what are the members going to be?

158
00:10:18,940 --> 00:10:21,610
They're going to be string name, right?

159
00:10:23,140 --> 00:10:25,480
And age and float.

160
00:10:25,840 --> 00:10:28,120
Wait, right.

161
00:10:28,120 --> 00:10:29,410
So there's our struct.

162
00:10:30,600 --> 00:10:34,950
The next thing I want to do is I think I'm going to add command line args, so I'm going to say and

163
00:10:34,950 --> 00:10:43,320
ARG C and Char are really so this will let us kind of specify the input file and the command line.

164
00:10:43,350 --> 00:10:47,400
So we can say person underscore input that here in the console.

165
00:10:48,720 --> 00:10:54,970
Since I'm doing that, the first thing I'm going to check is to see if ARG C is not equal to two.

166
00:10:54,990 --> 00:10:57,540
So that means they have not provided an input file.

167
00:10:57,540 --> 00:10:58,860
We will do our standard.

168
00:10:59,160 --> 00:11:01,140
You know, this is how you use the file.

169
00:11:01,410 --> 00:11:07,800
You're going to run the file name and input input file name here, something like that, right?

170
00:11:08,610 --> 00:11:11,190
And then we'll go ahead and exit if that's the case.

171
00:11:12,330 --> 00:11:17,580
So the next thing that I'm going to do is I'm going to go ahead and declare my input stream.

172
00:11:17,580 --> 00:11:29,340
So I'm going to say I have stream my stream and then I will do my stream open and we'll do our re one,

173
00:11:29,340 --> 00:11:29,550
right?

174
00:11:29,550 --> 00:11:35,350
Because that's going to be the command line argument will be at our be one in the argument vector,

175
00:11:35,350 --> 00:11:35,500
right?

176
00:11:35,520 --> 00:11:39,410
The position one next thing I'm going to do is make my vector.

177
00:11:39,420 --> 00:11:41,220
So what type is the vector going to be?

178
00:11:41,220 --> 00:11:42,510
It's going to be a vector of.

179
00:11:43,530 --> 00:11:49,200
People write so vector type person, a person is our struct name, we we a person right here.

180
00:11:49,650 --> 00:11:54,870
So Vector Person, I'm just going to call this person vector, you know, pretty original there.

181
00:11:56,400 --> 00:11:58,920
And then what else do we need to do?

182
00:11:59,640 --> 00:12:04,350
So we're going to read into some structure.

183
00:12:04,380 --> 00:12:06,060
We're going to want to loop through the file.

184
00:12:06,090 --> 00:12:11,220
What I'm going to do is actually makes I'm going to do this in two ways I'll show you, I'm actually

185
00:12:11,220 --> 00:12:14,160
going to do it in a few ways, probably more than two.

186
00:12:14,200 --> 00:12:18,120
You are going to show you kind of different ways that we can handle this problem.

187
00:12:20,190 --> 00:12:24,030
So what I do is make some variables first, right?

188
00:12:24,030 --> 00:12:29,880
I'm going to say string name, MSA and age, and I'm going to say float.

189
00:12:29,910 --> 00:12:32,520
Wait here, just straight up local variables.

190
00:12:32,530 --> 00:12:34,440
So this is one way that you could do this.

191
00:12:35,020 --> 00:12:38,580
And then what I'm going to do is I'm going to make a loop that loosened the file, and I'm just going

192
00:12:38,580 --> 00:12:47,280
to read into all three of these right here in the while so I can say my stream while we are able to

193
00:12:47,310 --> 00:12:49,320
read into our stream.

194
00:12:50,610 --> 00:12:57,450
A name followed by an age, followed by a week, so let's keep looping through the file as long as we

195
00:12:57,450 --> 00:13:03,180
can read all three of these into these variables with our input stream.

196
00:13:03,810 --> 00:13:05,580
So that is a loop that you can use.

197
00:13:06,390 --> 00:13:10,800
So we're just going to read into all of these variables, these local variables right off the bat.

198
00:13:10,800 --> 00:13:11,820
And then what do we do?

199
00:13:11,850 --> 00:13:15,480
Well, we can make a person each time, write a new person.

200
00:13:17,250 --> 00:13:22,470
So this is us just reading, and we know that the file format is name, age and weight.

201
00:13:22,470 --> 00:13:23,790
So that's why we're doing this.

202
00:13:23,800 --> 00:13:28,890
You know, it's kind of we're taking the easy way out because we know that name is followed by age is

203
00:13:28,890 --> 00:13:30,330
followed by wait in the file.

204
00:13:30,600 --> 00:13:34,880
So we're just reading them assuming it's all going to come in this order like this.

205
00:13:36,830 --> 00:13:43,340
And then I make a new person each time because each line, each name, age and weight line is a new

206
00:13:43,340 --> 00:13:44,040
person, right?

207
00:13:44,060 --> 00:13:48,440
So what I'm going to do is I'm going to now that I have a new instance of a person.

208
00:13:49,500 --> 00:13:54,300
Right, I made an instance of this struct, I can now set the variables right, so I'm going to say

209
00:13:54,300 --> 00:14:01,320
P nickname equals name because I already read in the data into this variable, right?

210
00:14:03,020 --> 00:14:07,460
The first line of the file is Bob, so I basically read Bob into here.

211
00:14:07,820 --> 00:14:09,580
I read Bob's Age and here.

212
00:14:10,850 --> 00:14:16,010
And then I read Bob's waiting to hear, and now I've created a new person, I can now set that person's

213
00:14:16,010 --> 00:14:21,860
attributes or name or variables to what the variables are already holding right here.

214
00:14:21,860 --> 00:14:24,800
The local variables are already holding the values for Bob, right?

215
00:14:25,340 --> 00:14:26,930
So Peter, name equals name Pete.

216
00:14:26,930 --> 00:14:29,270
Our age equals age.

217
00:14:29,600 --> 00:14:32,150
Pete weight equals weight.

218
00:14:33,860 --> 00:14:34,570
Now what?

219
00:14:34,580 --> 00:14:41,000
We filled a person instance p with all of the necessary data.

220
00:14:41,030 --> 00:14:48,740
Now what we can do is push it back to the person vector, a vector of type person called Person B person

221
00:14:48,980 --> 00:14:51,610
x push back P.

222
00:14:52,400 --> 00:14:57,830
We're adding the person to the person vector and this is all we really need to read it in.

223
00:14:57,830 --> 00:14:59,570
All we got to do now is print it out.

224
00:14:59,570 --> 00:15:05,330
So to say four in April, zero, I is less than however many items are in person bag.

225
00:15:05,350 --> 00:15:13,340
So a person like that size and then I plus plus what am I doing here is I'm going to say c out name

226
00:15:14,210 --> 00:15:17,990
and I'm going to say person backed in here.

227
00:15:18,050 --> 00:15:19,370
Think back to the lecture.

228
00:15:19,700 --> 00:15:21,710
We do person vector subscript.

229
00:15:21,710 --> 00:15:26,120
I dot name, right?

230
00:15:26,840 --> 00:15:27,860
And then an inline.

231
00:15:27,860 --> 00:15:29,230
So think back to this right.

232
00:15:29,240 --> 00:15:37,460
This person vector AI is going to resolve to the person instance, right?

233
00:15:37,480 --> 00:15:38,840
So each person, right?

234
00:15:39,230 --> 00:15:41,870
So for persons zero, that's going to be bob, right?

235
00:15:42,680 --> 00:15:44,330
So person vector AI.

236
00:15:45,310 --> 00:15:46,060
Gets us.

237
00:15:47,430 --> 00:15:51,570
You know, all of Bob Wright, the whole structure, the whole up person.

238
00:15:52,230 --> 00:15:58,170
And then we're going to say, OK, so p, this is Bob Bob not name is Bob right?

239
00:15:59,210 --> 00:16:05,270
So this whole thing right here resolves to a person we're substituting into the victor and getting the

240
00:16:05,270 --> 00:16:08,060
specific person out of the right position, right?

241
00:16:09,270 --> 00:16:17,420
So then I say, see our age and I'm going to say the same thing is going to be person that I but person

242
00:16:17,420 --> 00:16:18,620
that guy, age

243
00:16:21,560 --> 00:16:31,650
and line while I'm struggling to type C out and I'm going to say wait or struggle, person.

244
00:16:31,880 --> 00:16:36,980
In fact, I don't wait in line.

245
00:16:37,610 --> 00:16:40,040
And now we've turned out everything to do with the person.

246
00:16:40,040 --> 00:16:43,910
I'm going to add an extra new line here just to separate them a little bit more.

247
00:16:45,950 --> 00:16:47,660
So I have this in their.

248
00:16:49,320 --> 00:16:54,960
So this should be everything, let's go ahead and test it out, I do need to close my file, so I'm

249
00:16:54,960 --> 00:17:02,610
going to go ahead and close it here when I'm done, so I'll say my stream close right here.

250
00:17:06,500 --> 00:17:14,900
And yeah, we should have everything working, so I'm going to go ahead and save this and then I'm going

251
00:17:14,900 --> 00:17:20,540
to go hang a pilot, so I'll say Gee Plus Plus and this will be cigarettes.

252
00:17:20,900 --> 00:17:22,480
Five That CBP dash.

253
00:17:22,540 --> 00:17:26,160
Oh, and we call this five, see?

254
00:17:28,220 --> 00:17:28,520
All right.

255
00:17:28,520 --> 00:17:31,130
So compilers, OK, so what are we expecting from this?

256
00:17:31,160 --> 00:17:33,830
We're expecting it to print.

257
00:17:34,830 --> 00:17:38,840
Things on a new line for each person, so should say name.

258
00:17:39,660 --> 00:17:40,960
Bob, let's see.

259
00:17:41,100 --> 00:17:41,850
Let's just go to here.

260
00:17:41,880 --> 00:17:43,560
It's just a name.

261
00:17:43,950 --> 00:17:50,400
Bob, age 20, weight two hundred point five and then put a little new line like space.

262
00:17:50,970 --> 00:17:53,370
And then it's just a name, Nancy.

263
00:17:53,400 --> 00:17:57,090
Age 65, weight one or two point three, and so on.

264
00:17:57,090 --> 00:17:59,610
So let's go ahead and run this and double check that.

265
00:17:59,610 --> 00:18:00,480
That's what we get.

266
00:18:00,730 --> 00:18:00,890
Well.

267
00:18:02,670 --> 00:18:03,360
But down here.

268
00:18:07,250 --> 00:18:11,810
Oh, yeah, we forgot to put the file name, right, so let's go ahead and say person input.

269
00:18:13,520 --> 00:18:15,620
All right, so let's see what we got here.

270
00:18:15,650 --> 00:18:20,030
I'm going to scroll up so we can verify, and that looks like our file, right?

271
00:18:20,040 --> 00:18:24,320
So we have named Bob Age 20 Weight ten point five space.

272
00:18:24,320 --> 00:18:27,770
We got Nancy's info and then Jeff down here.

273
00:18:29,150 --> 00:18:30,760
So pretty cool, right?

274
00:18:30,770 --> 00:18:38,900
We can put these strokes into vectors and we can now read in from files and put stuff into our own abstract

275
00:18:38,900 --> 00:18:41,810
data types and store those in a list in a vector.

276
00:18:41,820 --> 00:18:45,680
So now we have multiple abstract data type instances, right?

277
00:18:46,010 --> 00:18:50,240
We have three different people stored inside of a container, right?

278
00:18:50,240 --> 00:18:51,200
They're stored in a vector.

279
00:18:51,200 --> 00:18:53,390
So that is something pretty cool.

280
00:18:53,390 --> 00:18:59,060
We're getting kind of better and better at being able to represent interesting ideas and code here.

281
00:19:00,380 --> 00:19:02,720
So I'm going to clear that and we'll go back to here.

282
00:19:03,830 --> 00:19:12,440
So another thing that I want to go over, though, is do we have to make a new person each loop?

283
00:19:13,010 --> 00:19:19,820
Could we just use the same instance of a person except each time we go through a loop?

284
00:19:21,040 --> 00:19:25,060
We change their data.

285
00:19:25,720 --> 00:19:30,760
We change the data of that person and then add it to the vector right in the next time we change the

286
00:19:30,760 --> 00:19:33,430
data of this person and add that to the vector.

287
00:19:34,420 --> 00:19:35,560
Would that work?

288
00:19:36,220 --> 00:19:37,900
So think about it.

289
00:19:38,140 --> 00:19:42,400
Are we referring to the same memory or are we making copies?

290
00:19:43,270 --> 00:19:45,300
So let's go ahead and try something else out.

291
00:19:45,310 --> 00:19:47,260
Let's go ahead and put the person here.

292
00:19:48,730 --> 00:19:52,300
And I'm not going to make a new person each live, I'm just going to use one person.

293
00:19:54,730 --> 00:20:01,840
And what I'm actually going to do is I'm going to read straight from the file into the data members

294
00:20:01,840 --> 00:20:04,090
instead of having these local variables.

295
00:20:04,510 --> 00:20:10,660
OK, so I'm going to actually delete all of this, and what I'm going to do is delete all of this as

296
00:20:10,660 --> 00:20:15,640
well, and we're literally only going to have this, but I'm going to put P Dot name here.

297
00:20:17,080 --> 00:20:20,920
PETA age here and PETA wait here.

298
00:20:23,190 --> 00:20:26,970
So what I'm doing is now I'm reading straight from the file.

299
00:20:28,000 --> 00:20:36,130
Into some reading from the files straight into the struct, right, so I made an instance of a person

300
00:20:36,130 --> 00:20:36,430
write.

301
00:20:37,600 --> 00:20:43,000
And I'm reading straight into the data member now right here, so we got the name goes straight into

302
00:20:43,000 --> 00:20:45,360
the data, remember the age goes straight into the data.

303
00:20:45,370 --> 00:20:47,500
Remember the wait goes straight into the data, remember?

304
00:20:47,800 --> 00:20:49,960
Now we've read into all three of these.

305
00:20:49,960 --> 00:20:54,040
What we do is we pushed that person into the vector.

306
00:20:54,340 --> 00:20:56,410
So that's now stored in that vector.

307
00:20:57,660 --> 00:21:02,550
Said the big question is, is it stored, is it a copy that's stored or?

308
00:21:03,940 --> 00:21:10,510
Is it going to have like, you know, are we going to keep changing that person like let's say, let's

309
00:21:10,510 --> 00:21:14,440
say we change the person each time, is it going to keep changing?

310
00:21:15,660 --> 00:21:21,930
Each one of the people inside the vector, so they all say, Jeff, because Jeff is the last one, we

311
00:21:21,930 --> 00:21:22,860
change it to, right?

312
00:21:23,010 --> 00:21:28,500
Is it going to change the already existing people that are stored in our vector of people?

313
00:21:29,530 --> 00:21:30,700
That is the question.

314
00:21:32,060 --> 00:21:33,950
So let's go ahead and run it and find out.

315
00:21:34,850 --> 00:21:40,070
So I'm going to go ahead and compile this, and then I'm going to go ahead and running again.

316
00:21:44,120 --> 00:21:46,820
So let's see what it says, we see Jeff there at the end.

317
00:21:46,910 --> 00:21:47,810
Let's scroll up.

318
00:21:48,200 --> 00:21:53,010
So no, it actually it actually saves copies.

319
00:21:53,030 --> 00:21:53,360
Right?

320
00:21:53,540 --> 00:21:58,220
And that is because we're not using a pointer to the person.

321
00:21:58,640 --> 00:22:01,810
We're not changing the same memory, right?

322
00:22:01,820 --> 00:22:09,980
Each time it's adding a new copy to the vector, except we're not explicitly making a new person here

323
00:22:09,980 --> 00:22:12,230
with our code like we're not doing this anymore.

324
00:22:14,100 --> 00:22:19,860
Right, so this is a slightly more efficient way to do it, it is still putting copies inside the vector.

325
00:22:20,220 --> 00:22:20,640
Right?

326
00:22:20,850 --> 00:22:23,610
So each person is going to be different in the vector.

327
00:22:24,940 --> 00:22:30,250
But what would it look like if we were referring to the same person with a pointer?

328
00:22:30,250 --> 00:22:36,460
So this is actually going to bring up a kind of new and more complex topic to do with strikes and in

329
00:22:36,460 --> 00:22:40,780
the next lecture, we are going to go over this in more detail.

330
00:22:41,920 --> 00:22:47,220
So what I'm going to do is just kind of introduce it now to kind of show you what it would look like.

331
00:22:47,230 --> 00:22:49,230
And then we're going to get into detail in the next lecture.

332
00:22:49,240 --> 00:22:54,370
So don't worry if I kind of leave you hanging a little bit on this, but let's just say, what if we

333
00:22:54,370 --> 00:22:55,720
had a pointer, right?

334
00:22:56,170 --> 00:23:00,250
How would we make a pointer to a person, right?

335
00:23:00,280 --> 00:23:03,760
What if we dynamically allocate a new person each time?

336
00:23:03,760 --> 00:23:08,890
So not only are we're making it just a pointer, but we're dynamically allocating it each time.

337
00:23:08,890 --> 00:23:17,780
So if I go person star piece and now it's a pointer and I say equals new person, right?

338
00:23:17,800 --> 00:23:22,810
We're doing it just like we've done previously with dynamically allocated memory right here.

339
00:23:22,810 --> 00:23:27,610
Is that dynamically allocated part and we have to make a pointer now since it's on the heap and we need

340
00:23:27,610 --> 00:23:28,270
to point to it.

341
00:23:28,990 --> 00:23:34,570
We need to change our vector now because it's not a vector of type person anymore, it's a vector of

342
00:23:34,570 --> 00:23:35,860
person pointers.

343
00:23:36,850 --> 00:23:39,130
So we're going to say vector of person pointers.

344
00:23:40,430 --> 00:23:48,110
Another interesting thing is this dot, we actually can't use this dot anymore to read into the name,

345
00:23:48,110 --> 00:23:48,830
age and weight.

346
00:23:48,980 --> 00:23:55,580
Since it's a pointer, we have to use a new weird little operator here and it's going to be an arrow,

347
00:23:55,580 --> 00:23:55,910
right?

348
00:23:55,920 --> 00:23:59,150
But it's an arrow with a minus and a greater than.

349
00:24:00,080 --> 00:24:03,690
It's a little bit weird, but you can think of it as pointing to something, right?

350
00:24:03,710 --> 00:24:06,530
It looks like a pointer like that you would think of in your mind.

351
00:24:06,530 --> 00:24:09,140
Like it's it's an arrow, an arrow points, right?

352
00:24:09,590 --> 00:24:12,620
So that is why we're going to use this syntax.

353
00:24:13,690 --> 00:24:18,190
It's a minus sign in a greater than symbol right afterwards, but you can think of it as an arrow because

354
00:24:18,190 --> 00:24:19,690
that's more so what it means, right?

355
00:24:19,690 --> 00:24:21,010
Because it's having to deal with a pointer.

356
00:24:21,010 --> 00:24:26,950
So we need to use this instead of a dot now since we're having a person pointer instead of just a normal

357
00:24:27,250 --> 00:24:28,480
person variable.

358
00:24:30,850 --> 00:24:34,390
So I'm going to do this arrow named Arrow AJPW Arrow.

359
00:24:34,420 --> 00:24:34,960
Wait.

360
00:24:36,030 --> 00:24:41,760
All right, then down here, we actually have to replace these dots as well, because this is going

361
00:24:41,760 --> 00:24:45,640
to resolve not to just a person, but a person pointer, right?

362
00:24:45,660 --> 00:24:46,290
Look at that.

363
00:24:46,500 --> 00:24:47,610
It hovers up here.

364
00:24:47,970 --> 00:24:52,020
It says Vector Person type person pointer.

365
00:24:52,020 --> 00:24:54,720
So we're actually going to need to put arrows here as well.

366
00:24:55,350 --> 00:24:58,680
And this is something that I'm going to get into more in the next lecture.

367
00:24:58,680 --> 00:24:59,840
So don't worry too much.

368
00:24:59,850 --> 00:25:05,670
I'm just kind of giving you a little introduction now to also show you what's going to happen if we're

369
00:25:05,670 --> 00:25:08,430
pointing to the same memory.

370
00:25:08,430 --> 00:25:14,940
What we're storing in the vector now is not a new person each time it's actually a new address.

371
00:25:16,360 --> 00:25:22,090
Not even a new address was stolen, a new pointer in that pointer, they're all pointing to the same

372
00:25:22,090 --> 00:25:26,920
memory location that stores the person, so this is going to yield some different results.

373
00:25:27,960 --> 00:25:29,910
So I'm going to go ahead and save this now.

374
00:25:31,040 --> 00:25:35,750
And let's see what happens, hopefully updated everything correctly to be able to use the pointer if

375
00:25:35,750 --> 00:25:37,760
we encounter some errors, we'll just have to fix them.

376
00:25:39,140 --> 00:25:41,720
So let me go ahead and recompile it.

377
00:25:41,720 --> 00:25:44,540
Looks like it compiled, OK, so let's go ahead and do step five.

378
00:25:45,080 --> 00:25:47,420
And now what do you think the output is going to be?

379
00:25:48,050 --> 00:25:49,580
So try and predict the output.

380
00:25:49,670 --> 00:25:51,290
We got these people in here, right?

381
00:25:53,280 --> 00:25:56,280
Go through the code in your head if it's a pointer.

382
00:25:57,950 --> 00:26:02,660
You know, what's going to happen each time when we start to the victor, it's just pointing to the

383
00:26:02,660 --> 00:26:04,310
same person that we made.

384
00:26:04,550 --> 00:26:10,400
So think about what this victor is going to say in the long run in the end of everything.

385
00:26:10,430 --> 00:26:11,480
So let's go ahead and run it.

386
00:26:15,150 --> 00:26:17,160
Well, that is interesting, isn't it?

387
00:26:17,550 --> 00:26:20,340
So look at this now, everything is Jeff.

388
00:26:22,040 --> 00:26:23,780
So why is everything, Jeff?

389
00:26:24,560 --> 00:26:30,410
Well, everything is Jeff, because Jeff is the last thing that we updated it to when I say updated

390
00:26:30,410 --> 00:26:30,620
it.

391
00:26:31,040 --> 00:26:31,490
Yes.

392
00:26:31,850 --> 00:26:33,230
There's only one person.

393
00:26:33,500 --> 00:26:39,020
And we're just literally storing a bunch of pointers pointing to the same person, right?

394
00:26:39,030 --> 00:26:46,490
So we point to some chunk of memory that has this structure, the person struct, we make a new dynamically

395
00:26:46,490 --> 00:26:47,930
allocated person.

396
00:26:49,160 --> 00:26:51,260
What we do is we read in from the file.

397
00:26:52,770 --> 00:26:54,870
Bob, twenty two hundred point five.

398
00:26:54,910 --> 00:27:03,870
Right, so we read all those in and we set the data of that person, we set the member variables of

399
00:27:03,870 --> 00:27:04,560
that person.

400
00:27:05,930 --> 00:27:07,100
It's all bad right now.

401
00:27:07,310 --> 00:27:10,130
What we do is we push back, not the person.

402
00:27:11,090 --> 00:27:17,780
But the person pointer to the vector, right, we're storing person pointers now, so we store a little

403
00:27:17,780 --> 00:27:21,380
memory address of where Bob exists.

404
00:27:22,630 --> 00:27:23,860
What do we do the next move?

405
00:27:23,880 --> 00:27:30,130
Well, we go in here, we're referring to the same piece of memory, we're pointing at Bob and now what

406
00:27:30,130 --> 00:27:31,390
we're doing is we're saying.

407
00:27:32,900 --> 00:27:38,480
Well, Bob's name is Nancy, actually, and he's this old and, you know, now it's like, she's this

408
00:27:38,480 --> 00:27:41,990
old and she has this wig, so it's not even Bob anymore.

409
00:27:41,990 --> 00:27:45,710
The same memory has been overwritten with Nancy's data.

410
00:27:48,360 --> 00:27:50,700
So it's been overwritten with Nancy's data.

411
00:27:51,660 --> 00:27:56,820
Then what we do is we come back the next time and we overwrite the person with Jeff, and we're doing

412
00:27:56,820 --> 00:28:00,950
that for each item in the vector because the vector is just storing the pointers, right?

413
00:28:00,960 --> 00:28:03,120
So why don't we loop through the vector?

414
00:28:03,390 --> 00:28:08,790
We're actually just going through a bunch of things that point to the same memory.

415
00:28:11,070 --> 00:28:11,430
Right.

416
00:28:12,970 --> 00:28:24,340
So if I was to change this to Nancy, all of these are just going to say Nancy instead of all of them

417
00:28:24,340 --> 00:28:25,390
saying Jeff.

418
00:28:26,230 --> 00:28:29,680
So they all say Jeff, right now, let's go ahead and run, and again, I'm not going to recompile it

419
00:28:29,680 --> 00:28:30,040
or anything.

420
00:28:30,040 --> 00:28:30,940
I'm just going to run again.

421
00:28:31,360 --> 00:28:31,900
Look at that.

422
00:28:31,900 --> 00:28:33,130
They'll say Nancy now.

423
00:28:33,130 --> 00:28:37,750
So it doesn't matter what comes before here, it's only the last thing that matters.

424
00:28:37,750 --> 00:28:40,930
They're all going to just be updated to the last thing in our list, right?

425
00:28:41,080 --> 00:28:42,220
The last thing in the file.
