OK, so in this lecture, we are actually going to introduce a new type of C++ container.
And this may be sort of a little bit of an advanced concept for this stage in the game, if you're just
starting out with C++ and you've just been following this series up until now.
This course until now.
But some of the things I'm going to mention, I'll just say that we're going to cover them in a future
portion of the class.
But I just wanted to mention this type of data structure, this container, so we can start using it
to solve some of our problems.
You don't necessarily need to fully understand how everything operates under the hood.
As far as this container, this vectors is what we're actually talking about this vector.
But just knowing how to use it right now is kind of what's important.
So I'll be covering the basics of it and then mentioning a few things just in case, because I feel
like I do need to say what it really is.
And, you know, in case you wanted to go further and research it for yourself, I can kind of at least
tell you what this container is, but it is something that will be using and will kind of slowly and
slowly unravel the mysteries behind how it is really implemented in code and how it works and all that.
So with that, let's go ahead and get started talking specifically about C++ vectors in this lecture.
So vectors are somewhat similar to arrays.
They are C++ standard template library containers, and they're similar to arrays and a couple ways
here and maybe a few more.
But one of the main things that's really noticeable is that you can access items and vectors and race
in the similar way with the bracket operator.
So vectors are not limited to accessing items in the container like this, but it is possible to do
it just like an array.
So, you know, an array that you might have something, for example, like a variable called my array.
So let's say your ray is called Myra and you were to use this subscript operator with these square brackets
and say, I want the item at position four to now be set to the value two.
So you can remember back to when you were using arrays, you would basically index the position of the
array, right?
And then you could change what's stored there by doing something like this.
You can do the same thing with the vector.
So right here we have pretty much the same code, except it's just called my vector, and we're just
pretending this is now a vector instead of an array.
And you could set the position for equal to two as well.
Another similar thing is they both do have elements stored sequentially in a contiguous manner.
Although sometimes vectors don't really have the elements in the same exact memory location all the
time because they are dynamic and they need to resize themselves once they get to full.
So that's something we'll talk about in the next slide.
As far as differences between arrays and vectors, but they do try and store things in this continuous
manner, how so?
And something to point out, vectors are something that are slightly more abstract than arrays.
Arrays are more primitive.
You can think of primitive data types like into the flow of arrays, a kind of just baked into the C++
language there.
Whereas these standard temporal library things were created by people to make C++ more robust, a slightly
more abstract.
So you had more functionality with some of these containers.
So a lot of people often use vectors and arrays interchangeably to solve similar problems, a lot of
lakes and algorithms that you might use, you could choose to use a vector or an array either or might
suffice for certain things.
I'm not saying it always will.
They do have differences, of course, and there are benefits and drawbacks to each, but they kind
of can help solve overlapping problems.
What I'm trying to say.
So how are they different from a race?
Well, technically vectors are something called a templated class.
That is something we will talk about in near future.
You do not need to know about templating glasses right now.
If you would like to go ahead and look that up and get ahead, then you're more than welcome to do that.
But this kind of goes over a more advanced concept that we're building up to so we can have a base to
kind of understand things better once we get to that point.
Nevertheless, it's useful to know how to use vectors right now, so that's why I'm introducing them.
So they have functions associated with him that kind of has to do with this class thing, classes and
functions, but that's something we're going to talk about later.
But right now you can just pretend that if you have a vector that you've declared just like an array,
imagine instead of being limited to what an array can do, you can also call a function on the array
that you created, which means it's not going to be an array.
It's going to be a vector.
The difference is that, I'm pointing out is that you were able to, for example, call functions on
a vector, whereas you might not be able to call functions on an array.
So.
That is a nice thing about Vector's is that they have this additional functionality that can help us
perform certain operations, for example, like adding or removing elements, we can use a function
to add or remove elements, and we don't have to worry too much about the underlying memory storage
of this thing.
Unlike an array where we need to know about it.
So they kind of function a little bit like dynamic areas as well because they are dynamic in nature.
They need to resize when necessary to accommodate more elements and kind of like I was talking about
the continuous memory thing, although that is true.
Unlike an array, it's not always going to be contiguous memory.
So an array is just like a block of memory.
You kind of just have a little pointer to the start of the array and you're just off kind of moving
in the offset of the data type size through that contiguous block of memory.
Whereas a vector things can move around to different addresses if it needs to resize than it needs to
find a larger piece of memory.
And this is something that we may expand on in the future, talking more about computer memory with
these certain parts of memory, like these stack in the heap and dynamic memory and all that.
But right now, just think of a victor as something that can kind of expand when it needs to automatically
to fit more things, and you don't really have to worry about it right now exactly how it works.
But just know that you can keep adding stuff to it and you don't really have to worry about you having
to recite it.
It'll just automatically resize for you.
So that's probably enough for us to get into it and start using vectors.
I think that's the best way for us to understand them.
So let's do that.
I'm kind of pulled up a little.
Terminal here.
Bash and we can make a small file to mess around with vectors.
I also have some documentation up here that I will refer to in a bit so we can look at all the functionality
of vectors and kind of what it is that makes them fancy arrays have.
So I have nothing in this directory.
I'm just going to go ahead and make something called vectors to copy a file and open it in the bin.
Text Editor.
So right off the bat, I know that I'm going to want to kind of show what is inside of the vectors so
we can kind of track what the vector looks like and what stuff is inside of it there to do that, we're
just going to print it out.
So I'm going to want to include a screen for that, right?
And just like I included stream, the way to get access to a vector is actually to include it from that
standard library.
So what we're going to do is include Vector, just like we did with our stream that will let us use
vectors in our program.
I'm just going to put it using namespace STD here since I'm just going to keep it simple and lazy and
I'm going to make a main function and just put it zero there.
And so how do we make a vector?
So it's similar to just like us making an integer right?
And then we could set an integer like this.
The only difference is that we need to add an extra little piece to it because an integer is just a
primitive data type.
Can we just defining it?
That is the type of data we're going to use, right?
We'll do the same with a vector if we want to say that, yes, we want a vector, but we also now need
to say what is the data type that the vector stores?
Is it a container of integers?
Is it a container of floats of doubles?
And you can pretty much put any type in here, and we're going to even kind of expand upon that later
on to realize that it's not limited to only integers and floats.
We're allowed to make our own data types, and that is something that we'll look into in the future.
But I wanted to mention that anything can go after this vector where here in between these less than
greater than in size.
So this would make a vector of integers.
Vector lesson and then end and then greater than they pretty much just.
Rapping around the words just like up here.
This has to do with that template thing that I mentioned.
And although we're not going over it right now, I just want to mention that that's kind of why you're
seeing this syntax here.
So if I want a vector of integers, I do that.
I just put it because that's how you would make an answer, right?
Well, when I wanted to make this integer and declare this since you called a I put the word int before
it, right?
So the same thing here in the vector, that's like what I'm doing.
That tells C++ that I'm about to make a vector integers.
I can do the same thing with a double or float.
Yeah, know I was going to make integer right now to be simple.
Now you can name it just like you would any other variable.
So I called my vector and this just declares the vector.
It's not really initialized yet.
So there's nothing really in the vector.
It's basically just empty.
So if I was to see out and my vector and try and access like the first item in it, that would be a
problem.
So let's see like what that would be.
So I'm going to compile it just by doing G Plus Plus vectors that keep this a default to just call it
out, of course.
Oops!
Yeah, it was called my vet.
OK, so we compiled it now.
Now we have eight on out, but when I run it, it gets a segmentation fault.
And this is basically just saying that we tried to access something out of bounds in this case.