We've seen a raise before which are contiguous size containers they contain whatever type that we send
it to.
We've also seen how to use or how to resize arrays by using dynamic memory.
As we saw in the mini project well viscose plus standard has a type that can do all of all of this for
us already built.
So it's called a vector.
And don't use the name vector with linear algebra vectors.
These are completely different.
This C++ vector is effectively a dynamic array.
It will resize itself if you add more elements then had a capacity for the most interesting thing about
vectors is that you can use them with any type.
So how to use a vector.
We have to say clewed vector.
This day and if we wanted say a vector of integers we'd say vector in which this man gave it been.
OK.
So we have some new syntax here.
These kind of angle brackets right here k.
They're part of what is called templates and templates is a very very deep topic and far beyond the
scope of this course but just know that if we want to make a vector in between these brackets here is
the type that we want to hold in the vector k.
So the entire thing this whole thing right here right here is the type.
OK.
So it's a vector of integers.
In this case and this vector of integers we can actually add things to it.
So if you wanted to add an integer at the back of the vector we could do this say it back and say push
back five k.
But say so this will actually add 5 to the end of the vector.
OK.
And I like to for my mental model of vectors it's really just the array and it's really just the array
mental model.
And same with strings arranged have the array mental models but with characters instead.
And one nice thing is that you can get the size of the vector.
So if I said see Ouch the size vector in that size x K This should give me one so the size of the vector
you want.
Right.
So I have one element that I've added to the vector K and I could even get the capacity
passively of the vector.
So this is how many elements that are actually allocated in memory.
Ok so if I say that it passive vector currently is one capacity your own if we wanted to have more capacity
we could actually reserve it so we can actually say back Reserve and that's reserved 20 k.
So the reserve will actually make the capacity of the vector at least big enough to contain an elements
or in this case 20 it's OK.
So if you're on this we see the capacity has changed to be 20 so there's 20 elements.
OK.
And you might ask OK what happens if I add more more elements then the capacity.
OK.
So I did that say this zero is less than say 20 in this case so they'll be 21 at the end.
This say in at
to push back.
So you're right this so size is 21 and noticed that the capacity change so it changed to 40.
And so it actually doubled the size of the capacity when we went over.
In fact let me do this again and see it change at some point.
OK.
So let's just come at this.
Ouch.
So each time we add We'll check to see the size and to see if the capacity change through this
clips do it.
OK.
So the size of the vector is to 20 20 20 20 20 20 k up until we add one more which then it doubled.
Right.
So once we added the twenty first element then the capacity changed K so it regrew the entire internal
array that it uses to store all the integers.
So that's that's really what a vector is at its heart.
So it's dynamically changing array and then growing array.
We can also say remove something from the vector say Interac.
Pop back and doesn't take any arguments.
And this will actually remove the last element
in the vector
so if we said we just comment this
we should have 20.
It's the sizes 20.
And those of the capacity didn't change because it already knows that we can have more than that right.
In our programs of our.
And of course we can check.
Well we could actually before we do that we could actually remove all the elements in the vector.
So it's a Interac not clear.
So this will remove all the elements in the vector gay and we can also check to see if the vector is
empty.
This or turn true if it's empty.
So we say
actor is empty
else.
Or.
So this should only print that the is empty at the act.
So it's an and after the clear we see that the vector is empty.
So the size is zero.
In this case and one last thing just like arrays we can actually set individual elements of the vector
using this indexing operator.
And just like arrays.
So I say 0 is 5.
Say and I can say See.
Ouch.
0 is
x.
This.
You see that act of 0 5.
So you can access individual elements just like arrays.
These will be the main functions we'll use in our projects.
So I show you a few more that will be needing in our mini project.
And if you want to see what else the vector type can do you can actually check out this link here.
And you can see all the other functions that the vector has.
Generally speaking vectors are far safer than using arrays and especially dynamically allocated trees
and they're more convenient since since a lot of the work will be taken care of for you.
For example resizing your array.
One last thing is they're use with functions.
So just like strings you want to pass a vector by reference k
k.
So if you want to add to it or change it in any way you should always be passing it by reference.
Otherwise if you pass it by value it will do that kind of very expensive copy that we saw with strings.
Or again if you want to not be able to change it you should pass it by constant reference so that's
really yeah.
And we'll be seeing some more examples of vectors and strings in the upcoming finale of The Mindy Project
and and also in the game Space Invaders clone that we're right.
And so I want to talk about sorting vectors in the next lecture.