1
00:00:00,640 --> 00:00:11,170
OK, so in this lecture, we are actually going to introduce a new type of C++ container.

2
00:00:12,460 --> 00:00:20,230
And this may be sort of a little bit of an advanced concept for this stage in the game, if you're just

3
00:00:20,230 --> 00:00:24,940
starting out with C++ and you've just been following this series up until now.

4
00:00:25,600 --> 00:00:26,920
This course until now.

5
00:00:27,970 --> 00:00:34,390
But some of the things I'm going to mention, I'll just say that we're going to cover them in a future

6
00:00:34,390 --> 00:00:35,350
portion of the class.

7
00:00:35,770 --> 00:00:44,230
But I just wanted to mention this type of data structure, this container, so we can start using it

8
00:00:44,230 --> 00:00:46,090
to solve some of our problems.

9
00:00:46,690 --> 00:00:53,230
You don't necessarily need to fully understand how everything operates under the hood.

10
00:00:54,190 --> 00:00:59,680
As far as this container, this vectors is what we're actually talking about this vector.

11
00:01:00,790 --> 00:01:07,500
But just knowing how to use it right now is kind of what's important.

12
00:01:07,510 --> 00:01:12,910
So I'll be covering the basics of it and then mentioning a few things just in case, because I feel

13
00:01:12,910 --> 00:01:16,030
like I do need to say what it really is.

14
00:01:16,550 --> 00:01:24,520
And, you know, in case you wanted to go further and research it for yourself, I can kind of at least

15
00:01:24,520 --> 00:01:31,570
tell you what this container is, but it is something that will be using and will kind of slowly and

16
00:01:31,570 --> 00:01:38,530
slowly unravel the mysteries behind how it is really implemented in code and how it works and all that.

17
00:01:38,560 --> 00:01:45,190
So with that, let's go ahead and get started talking specifically about C++ vectors in this lecture.

18
00:01:46,960 --> 00:01:49,750
So vectors are somewhat similar to arrays.

19
00:01:50,050 --> 00:01:59,110
They are C++ standard template library containers, and they're similar to arrays and a couple ways

20
00:01:59,110 --> 00:02:00,690
here and maybe a few more.

21
00:02:00,700 --> 00:02:07,090
But one of the main things that's really noticeable is that you can access items and vectors and race

22
00:02:07,090 --> 00:02:10,230
in the similar way with the bracket operator.

23
00:02:11,460 --> 00:02:21,000
So vectors are not limited to accessing items in the container like this, but it is possible to do

24
00:02:21,000 --> 00:02:22,110
it just like an array.

25
00:02:22,710 --> 00:02:29,180
So, you know, an array that you might have something, for example, like a variable called my array.

26
00:02:29,190 --> 00:02:36,510
So let's say your ray is called Myra and you were to use this subscript operator with these square brackets

27
00:02:37,080 --> 00:02:44,520
and say, I want the item at position four to now be set to the value two.

28
00:02:44,970 --> 00:02:53,490
So you can remember back to when you were using arrays, you would basically index the position of the

29
00:02:53,490 --> 00:02:54,300
array, right?

30
00:02:54,300 --> 00:02:58,230
And then you could change what's stored there by doing something like this.

31
00:02:58,740 --> 00:03:00,780
You can do the same thing with the vector.

32
00:03:00,930 --> 00:03:07,530
So right here we have pretty much the same code, except it's just called my vector, and we're just

33
00:03:07,530 --> 00:03:10,140
pretending this is now a vector instead of an array.

34
00:03:11,140 --> 00:03:15,460
And you could set the position for equal to two as well.

35
00:03:16,540 --> 00:03:22,000
Another similar thing is they both do have elements stored sequentially in a contiguous manner.

36
00:03:23,710 --> 00:03:33,640
Although sometimes vectors don't really have the elements in the same exact memory location all the

37
00:03:33,640 --> 00:03:43,330
time because they are dynamic and they need to resize themselves once they get to full.

38
00:03:44,250 --> 00:03:46,710
So that's something we'll talk about in the next slide.

39
00:03:47,430 --> 00:03:53,970
As far as differences between arrays and vectors, but they do try and store things in this continuous

40
00:03:54,480 --> 00:03:55,860
manner, how so?

41
00:03:57,580 --> 00:04:05,380
And something to point out, vectors are something that are slightly more abstract than arrays.

42
00:04:05,830 --> 00:04:07,390
Arrays are more primitive.

43
00:04:07,930 --> 00:04:13,150
You can think of primitive data types like into the flow of arrays, a kind of just baked into the C++

44
00:04:13,150 --> 00:04:14,020
language there.

45
00:04:14,030 --> 00:04:20,920
Whereas these standard temporal library things were created by people to make C++ more robust, a slightly

46
00:04:20,920 --> 00:04:21,790
more abstract.

47
00:04:22,210 --> 00:04:25,120
So you had more functionality with some of these containers.

48
00:04:26,630 --> 00:04:31,310
So a lot of people often use vectors and arrays interchangeably to solve similar problems, a lot of

49
00:04:31,310 --> 00:04:38,270
lakes and algorithms that you might use, you could choose to use a vector or an array either or might

50
00:04:38,270 --> 00:04:39,740
suffice for certain things.

51
00:04:39,750 --> 00:04:41,540
I'm not saying it always will.

52
00:04:41,550 --> 00:04:47,720
They do have differences, of course, and there are benefits and drawbacks to each, but they kind

53
00:04:47,720 --> 00:04:51,220
of can help solve overlapping problems.

54
00:04:51,320 --> 00:04:52,220
What I'm trying to say.

55
00:04:54,790 --> 00:04:56,620
So how are they different from a race?

56
00:04:56,650 --> 00:05:01,090
Well, technically vectors are something called a templated class.

57
00:05:01,120 --> 00:05:03,380
That is something we will talk about in near future.

58
00:05:03,400 --> 00:05:06,790
You do not need to know about templating glasses right now.

59
00:05:07,150 --> 00:05:12,880
If you would like to go ahead and look that up and get ahead, then you're more than welcome to do that.

60
00:05:13,330 --> 00:05:22,060
But this kind of goes over a more advanced concept that we're building up to so we can have a base to

61
00:05:22,060 --> 00:05:25,510
kind of understand things better once we get to that point.

62
00:05:26,170 --> 00:05:32,860
Nevertheless, it's useful to know how to use vectors right now, so that's why I'm introducing them.

63
00:05:35,560 --> 00:05:42,250
So they have functions associated with him that kind of has to do with this class thing, classes and

64
00:05:42,250 --> 00:05:44,530
functions, but that's something we're going to talk about later.

65
00:05:44,530 --> 00:05:50,440
But right now you can just pretend that if you have a vector that you've declared just like an array,

66
00:05:51,010 --> 00:06:01,750
imagine instead of being limited to what an array can do, you can also call a function on the array

67
00:06:01,750 --> 00:06:05,290
that you created, which means it's not going to be an array.

68
00:06:05,710 --> 00:06:06,940
It's going to be a vector.

69
00:06:07,630 --> 00:06:13,090
The difference is that, I'm pointing out is that you were able to, for example, call functions on

70
00:06:13,090 --> 00:06:17,230
a vector, whereas you might not be able to call functions on an array.

71
00:06:17,680 --> 00:06:18,340
So.

72
00:06:20,310 --> 00:06:26,310
That is a nice thing about Vector's is that they have this additional functionality that can help us

73
00:06:26,310 --> 00:06:33,000
perform certain operations, for example, like adding or removing elements, we can use a function

74
00:06:33,000 --> 00:06:39,210
to add or remove elements, and we don't have to worry too much about the underlying memory storage

75
00:06:39,210 --> 00:06:40,410
of this thing.

76
00:06:41,220 --> 00:06:44,550
Unlike an array where we need to know about it.

77
00:06:46,210 --> 00:06:52,420
So they kind of function a little bit like dynamic areas as well because they are dynamic in nature.

78
00:06:53,830 --> 00:06:59,590
They need to resize when necessary to accommodate more elements and kind of like I was talking about

79
00:06:59,590 --> 00:07:02,530
the continuous memory thing, although that is true.

80
00:07:02,770 --> 00:07:07,830
Unlike an array, it's not always going to be contiguous memory.

81
00:07:07,840 --> 00:07:11,110
So an array is just like a block of memory.

82
00:07:11,420 --> 00:07:18,400
You kind of just have a little pointer to the start of the array and you're just off kind of moving

83
00:07:18,400 --> 00:07:22,810
in the offset of the data type size through that contiguous block of memory.

84
00:07:23,500 --> 00:07:29,020
Whereas a vector things can move around to different addresses if it needs to resize than it needs to

85
00:07:29,020 --> 00:07:31,060
find a larger piece of memory.

86
00:07:32,620 --> 00:07:39,430
And this is something that we may expand on in the future, talking more about computer memory with

87
00:07:39,820 --> 00:07:45,610
these certain parts of memory, like these stack in the heap and dynamic memory and all that.

88
00:07:46,740 --> 00:07:54,750
But right now, just think of a victor as something that can kind of expand when it needs to automatically

89
00:07:54,750 --> 00:08:00,570
to fit more things, and you don't really have to worry about it right now exactly how it works.

90
00:08:01,050 --> 00:08:07,230
But just know that you can keep adding stuff to it and you don't really have to worry about you having

91
00:08:07,230 --> 00:08:08,020
to recite it.

92
00:08:08,020 --> 00:08:10,530
It'll just automatically resize for you.

93
00:08:11,850 --> 00:08:17,790
So that's probably enough for us to get into it and start using vectors.

94
00:08:18,120 --> 00:08:20,640
I think that's the best way for us to understand them.

95
00:08:22,320 --> 00:08:23,180
So let's do that.

96
00:08:23,190 --> 00:08:24,690
I'm kind of pulled up a little.

97
00:08:26,010 --> 00:08:27,340
Terminal here.

98
00:08:27,990 --> 00:08:33,230
Bash and we can make a small file to mess around with vectors.

99
00:08:33,840 --> 00:08:40,560
I also have some documentation up here that I will refer to in a bit so we can look at all the functionality

100
00:08:40,560 --> 00:08:46,120
of vectors and kind of what it is that makes them fancy arrays have.

101
00:08:46,140 --> 00:08:49,790
So I have nothing in this directory.

102
00:08:49,800 --> 00:08:55,380
I'm just going to go ahead and make something called vectors to copy a file and open it in the bin.

103
00:08:55,410 --> 00:08:56,190
Text Editor.

104
00:08:58,760 --> 00:09:04,160
So right off the bat, I know that I'm going to want to kind of show what is inside of the vectors so

105
00:09:04,160 --> 00:09:09,590
we can kind of track what the vector looks like and what stuff is inside of it there to do that, we're

106
00:09:09,590 --> 00:09:10,700
just going to print it out.

107
00:09:11,270 --> 00:09:15,350
So I'm going to want to include a screen for that, right?

108
00:09:16,460 --> 00:09:24,740
And just like I included stream, the way to get access to a vector is actually to include it from that

109
00:09:24,740 --> 00:09:25,790
standard library.

110
00:09:25,800 --> 00:09:31,910
So what we're going to do is include Vector, just like we did with our stream that will let us use

111
00:09:31,910 --> 00:09:33,050
vectors in our program.

112
00:09:34,730 --> 00:09:42,650
I'm just going to put it using namespace STD here since I'm just going to keep it simple and lazy and

113
00:09:42,650 --> 00:09:49,470
I'm going to make a main function and just put it zero there.

114
00:09:49,490 --> 00:09:52,460
And so how do we make a vector?

115
00:09:53,270 --> 00:09:56,540
So it's similar to just like us making an integer right?

116
00:09:56,540 --> 00:09:58,640
And then we could set an integer like this.

117
00:10:00,260 --> 00:10:06,950
The only difference is that we need to add an extra little piece to it because an integer is just a

118
00:10:06,950 --> 00:10:07,960
primitive data type.

119
00:10:08,210 --> 00:10:10,270
Can we just defining it?

120
00:10:10,280 --> 00:10:12,370
That is the type of data we're going to use, right?

121
00:10:12,380 --> 00:10:19,250
We'll do the same with a vector if we want to say that, yes, we want a vector, but we also now need

122
00:10:19,250 --> 00:10:23,420
to say what is the data type that the vector stores?

123
00:10:23,930 --> 00:10:26,570
Is it a container of integers?

124
00:10:26,900 --> 00:10:30,350
Is it a container of floats of doubles?

125
00:10:30,830 --> 00:10:36,650
And you can pretty much put any type in here, and we're going to even kind of expand upon that later

126
00:10:36,650 --> 00:10:41,120
on to realize that it's not limited to only integers and floats.

127
00:10:41,780 --> 00:10:47,240
We're allowed to make our own data types, and that is something that we'll look into in the future.

128
00:10:47,600 --> 00:10:55,700
But I wanted to mention that anything can go after this vector where here in between these less than

129
00:10:55,700 --> 00:10:56,640
greater than in size.

130
00:10:56,720 --> 00:11:00,170
So this would make a vector of integers.

131
00:11:01,190 --> 00:11:05,720
Vector lesson and then end and then greater than they pretty much just.

132
00:11:06,970 --> 00:11:09,520
Rapping around the words just like up here.

133
00:11:10,630 --> 00:11:14,380
This has to do with that template thing that I mentioned.

134
00:11:14,800 --> 00:11:19,480
And although we're not going over it right now, I just want to mention that that's kind of why you're

135
00:11:19,480 --> 00:11:21,130
seeing this syntax here.

136
00:11:22,990 --> 00:11:25,270
So if I want a vector of integers, I do that.

137
00:11:25,270 --> 00:11:28,020
I just put it because that's how you would make an answer, right?

138
00:11:28,490 --> 00:11:35,140
Well, when I wanted to make this integer and declare this since you called a I put the word int before

139
00:11:35,140 --> 00:11:35,590
it, right?

140
00:11:36,460 --> 00:11:39,070
So the same thing here in the vector, that's like what I'm doing.

141
00:11:40,240 --> 00:11:43,450
That tells C++ that I'm about to make a vector integers.

142
00:11:43,450 --> 00:11:47,680
I can do the same thing with a double or float.

143
00:11:48,040 --> 00:11:51,400
Yeah, know I was going to make integer right now to be simple.

144
00:11:52,660 --> 00:11:55,540
Now you can name it just like you would any other variable.

145
00:11:55,550 --> 00:11:58,870
So I called my vector and this just declares the vector.

146
00:11:59,200 --> 00:12:01,000
It's not really initialized yet.

147
00:12:01,540 --> 00:12:03,340
So there's nothing really in the vector.

148
00:12:03,350 --> 00:12:05,680
It's basically just empty.

149
00:12:06,580 --> 00:12:16,570
So if I was to see out and my vector and try and access like the first item in it, that would be a

150
00:12:16,570 --> 00:12:17,380
problem.

151
00:12:17,500 --> 00:12:19,220
So let's see like what that would be.

152
00:12:22,740 --> 00:12:28,660
So I'm going to compile it just by doing G Plus Plus vectors that keep this a default to just call it

153
00:12:29,130 --> 00:12:30,180
out, of course.

154
00:12:35,530 --> 00:12:35,910
Oops!

155
00:12:38,290 --> 00:12:39,720
Yeah, it was called my vet.

156
00:12:43,870 --> 00:12:45,970
OK, so we compiled it now.

157
00:12:46,000 --> 00:12:53,470
Now we have eight on out, but when I run it, it gets a segmentation fault.

158
00:12:53,470 --> 00:12:57,730
And this is basically just saying that we tried to access something out of bounds in this case.
