This is basically just saying that we tried to access something out of bounds in this case, the vector
basically looked like this.
It was just empty and we were just like, Get me the first thing out of this empty vector, as if there
was some type of number in there like that.
But there wasn't.
And so we got a segfault.
So we declared our vector, but we didn't really declare like a size or anything, right?
We didn't make it like an array.
We just gave it like a variable name.
How do we put stuff in our vector?
Well, we actually can use one of these nifty functions that I was talking about and the way that we
use a function with the vector, like a special function that's meant for vectors is to use a dot with
the vectors name.
So I can say in my vest and then I use a dot to access the function.
And the first function I'm going to mention is something called push underscore it back.
So, you know, this is a function call because it has these parentheses right here in parentheses.
Push back, just add something to the end of the vector.
So whatever, I want to add an end of the vector I put as an argument to the function here.
So if I put three, it's going to take our vector from being something like this
to being something like this.
So it's going to add that three to it, if I was to call a push back again and put three in it again,
then it would put another one, yet another three at the end of the victory, and it would look like
that.
So just something to remember when you want to call one of these functions on the vector, you put this
dot here in this specific function called push underscore back.
Its purpose is to put whatever we took or whatever we put in here is going to put it at the end of our
vector.
And now that we have something in our vector, it basically went from being empty like this right to
now having this three in it.
That means that we do have something at positions here, right?
So this should not be a problem anymore.
We should be able to access this first and only item in our vector.
So let's confirm that let's make sure that we this victory that we declare here, let's make sure that
it actually did add this three to it.
And then here, if that was true, then what this should print out is just the number three.
So I'm going to re compile.
And I'm going to run it on out again.
And there we see that we get our three.
So it was added to the record, just to be even more sure.
I'm going to go ahead and use another function now.
So.
Well, I'm going to do is use a function called size, so I'm going do my vegan diet right to access
the function and then I'm going to use the size function and it just doesn't take any parameters in
here.
And what it's going to do is actually give me the current size of the vector.
So if our vector has one thing in it at position zero, just one thing the size will be one.
In fact, Victor has no things in it, if it's empty, it'll be so zero director has three things in
it, it would be so three.
So on and so forth.
So I want to print that out.
I'm going to say see out my right the size of the first line that prints out should be the size of the
vector.
And you know what I'll do.
I'll actually say I was criticized here at
a little space.
And then let's also copy paste this line, and we will put it.
Put it right here, so I'll also say that because zero.
Right here.
So that way, we can see what printout we can explain what we're printing out.
So let's see what the size is once I declare it right after I declare my victory.
We'll get the size, then we'll push back three.
Then we'll see that it's stored in there and then we'll see what the size is after I went to recompile
around eight on out.
OK, so here we go, look at this, so it says size is zero right after we just declared it right,
then what we did was we pushed back a three to the vector in between these two things.
And now and then we printed out what was at the first position in the vector, which is only at three
in it and the thing at position zero three.
Then we printed out the size now, and it said the size is one because it has one item in it.
All right,
cool.
So we can see that there's some pretty cool functionality with this that kind of sets it apart from
just an array, right?
We just were able to declare it right here and then we can just start adding stuff to it and getting
the information about how many things are in it.
Pretty, pretty neat, honestly.
So let's go ahead.
I'm going to add another item to it, so I'll say my right that push back, and I'm just going to add
this for to it.
And then we'll put another size right here.
And then let's use even yet another function, so I'm going to say my back and this is just like push
back, except it's kind of the opposite.
Instead of adding something, it just reduces the size of the vector by one, and that is called pop
effect.
So that's basically reducing the size of the vector by one.
So we'll put a pop back in here and I will put another size right here.
So we'll call, let's just say, size after pop that.
And so we have another pushback here.
Maybe you will actually print this out to.
Okay, so
we'll do my next one.
All right, because once we have this next item in it, we push back at three and that's it in zero.
And then we get a size and then push back a four.
So the vector is basically looking something like this three as it is at position zero four, is it
position one?
So that's why we all do this position one.
Print that out.
And then we'll get this size after the putback, so we'll pop back and then we'll get that size, so
it should be something like size zero prints out three sizes, one pushes back.
Here it prints out four because that's the thing we push back.
And then here it should say that the size is two, because there's two things in it.
We pop back in and the size should not be two anymore should go back to one.
So let's compile this and let's run it.
So there we see size starts out as zero.
Then what we did is we added three, and it shows the three, and then it says size is one.
Then we added a four to it and we print out my next one in a four.
Then we say, What's the size now?
The size is two.
Then we do a pop back and then the size after the pop back, it says now is back to one
cool.
So what if I don't really know how many items are in my vector, but I just want to know what's at the
end of the vector?
So rather than doing like my Vector zero and my vector one, I can use yet another function.
And this is going to be my vector back, and that'll just give me whatever item is at the very end of
the vector.
So now this should print out the same thing.
The same thing as we had before, but instead, we're using this function, we're not acting like we
know what's in the vector.
We're just pretending anything could have gotten added.
There could be whatever elements, but we want to know what's at the back each time.
Right.
We want to know what the very last thing in the vector is.
So we have this right here is what came out previously.
Let's go ahead and re compile and then let's run it again.
And so we notice, you know, here, I didn't put the my big zero in my wrecked one, but we got the
same results as before, right?
Size is zero.
Then we print out the last thing in it, which is there's only one thing, which is three that says,
Hey, the size is one.
Then we added the four and we printed out the last thing in it.
And that's four because there's two items now.
And then we said, What's the size?
The size is, too.
Then we did a pop back and then it says the size after the pop back is one.
So pretty cool.
We noticed there's quite a bit of functionality here with the vector.
So whenever you want to call some of these functions on the vector, just remember that you're going
to use that dot and then the name of the function.
So how do you figure out all of the functions?
Where do you and where can you find a list of all of these functions you can use with your vector?
That's something like right here that you can refer to, for example, C++ dot com.
You want to see if I can enlarge this a bit.
OK, so we can see a little better.
So here we have pushed back and putt putt putt putt back right here.
You notice there some example code where they're pushing back 100, pushing back 200, 300 and all that.
Here's actually a nice list of all of these functions.
These are called member functions because they are members of the class.
But remember, don't focus too hard on that right now.
We're going to get into talking about classes and all that.
Just know that you can put a dot and then each one of these names here.
To do kind of different things like, let's look at this right here clear.
This tells us what the clear function does, removes all elements from the vector which are destroyed,
leaving the container with the size of zero.
Huh.
Interesting.
So what if we go back to our code here and we have size after pop back, right?
Because we did this pop back, our size was too right here.
What if instead of pop back, we just did clear.
So our size after pop back was one, right?
So now let's see what our size is actually clear should be zero, right?
So let's run it in there.
You notice the size was two.
We did our clear function and it says size after a career is zero, so it removed everything from the
vector.
Cool, so, yeah, there's a lot more functions you can use here.
And this is just kind of supposed to make your life a little bit easier.
You know, arrays are great and this great to great practice to use them.
And sometimes they have benefits over a vector and over other data structures that will go over.
I won't go into that too much right now about, you know, how much space stuff takes up and how fast
things are and efficient things are.
That's something that we're going to go deep into later in the course.
But right now, just know that this is kind of a fancy array that you can use, if you would like,
you just kind of declare it like this here.
Of course they're not using.
Well, namespace, they're putting this study, Colin Colin, with the scope resolution here.
To be able to use the vector.
But just like we did, they made a vector of integers, they call it my vector rather than my vector.
And they did make sure to push back here a few times.
Here we have some looping stuff that you can kind of ignore.
But then they do clear and they do more push backs here.
So it's just kind of demonstrates, you know, they have a nice little code snippets here that demonstrate
how to use these functions.
And here you notice there's some other things he is like back.
We also have begin.
So actually, let's look at front.
So a reference to the front of the vector, so we can't even try that out.
What about we have this?
My vector back, right?
So let's just say
back of the vector.
And then.
Put the same thing here.
Oops.
So we'll say the back of the vector is right here and then.
Let's also do a.
Front of the victory, so as to see out front of the.
So this will be like my ex got front.
So this is where we had three in it, right?
And then we also push back a four.
It'll show the back of the picture, which is four, because that's at the end and then the front should
be a three, right?
So let's go ahead and compile this and run it.
Cool.
So there you see that my dog front was able to get us what was in the front of the victor.
So basically, it was like you, we pushed back the three, right, and that's what's in the back,
it said, besides this one.
Then we pushed back a four before this line and we say, what's at the back?
It's a four.
We just added that what's at the front of the vector?
That's three that three is still in.
There is the first thing right in the size is two.
And then we cleared it in the Air Force ActionScript zero for size.
All right.
So that's pretty much all.
I'm going to go over for the vectors right now.
Be really great to look over this reference right here and read up on them this this code might have
some things that you are maybe not as comfortable with or might not know about yet, and that's totally
fine.
Just kind of take it for what it is.
And, you know, so look at the code that it makes sense to you, like some of some stuff that we've
only covered yet, you know, you can kind of just like, look at how that's used.
If you see something else that doesn't make sense, you don't need to try and use that.