WEBVTT

00:00.620 --> 00:08.990
We've seen a raise before which are contiguous size containers they contain whatever type that we send

00:08.990 --> 00:10.220
it to.

00:10.220 --> 00:16.430
We've also seen how to use or how to resize arrays by using dynamic memory.

00:16.450 --> 00:26.740
As we saw in the mini project well viscose plus standard has a type that can do all of all of this for

00:26.740 --> 00:29.140
us already built.

00:29.140 --> 00:31.720
So it's called a vector.

00:31.870 --> 00:36.400
And don't use the name vector with linear algebra vectors.

00:36.430 --> 00:38.310
These are completely different.

00:38.320 --> 00:43.780
This C++ vector is effectively a dynamic array.

00:43.780 --> 00:51.930
It will resize itself if you add more elements then had a capacity for the most interesting thing about

00:51.930 --> 00:56.730
vectors is that you can use them with any type.

00:56.730 --> 00:59.290
So how to use a vector.

00:59.320 --> 01:05.600
We have to say clewed vector.

01:05.630 --> 01:20.330
This day and if we wanted say a vector of integers we'd say vector in which this man gave it been.

01:20.570 --> 01:21.190
OK.

01:21.610 --> 01:23.980
So we have some new syntax here.

01:23.980 --> 01:28.750
These kind of angle brackets right here k.

01:29.450 --> 01:39.570
They're part of what is called templates and templates is a very very deep topic and far beyond the

01:39.570 --> 01:51.690
scope of this course but just know that if we want to make a vector in between these brackets here is

01:51.690 --> 01:55.080
the type that we want to hold in the vector k.

01:55.110 --> 02:02.410
So the entire thing this whole thing right here right here is the type.

02:02.710 --> 02:03.260
OK.

02:03.360 --> 02:05.940
So it's a vector of integers.

02:05.940 --> 02:13.540
In this case and this vector of integers we can actually add things to it.

02:13.620 --> 02:25.470
So if you wanted to add an integer at the back of the vector we could do this say it back and say push

02:25.470 --> 02:30.250
back five k.

02:30.910 --> 02:37.490
But say so this will actually add 5 to the end of the vector.

02:37.840 --> 02:38.420
OK.

02:38.670 --> 02:48.480
And I like to for my mental model of vectors it's really just the array and it's really just the array

02:48.480 --> 02:49.290
mental model.

02:49.350 --> 02:56.460
And same with strings arranged have the array mental models but with characters instead.

02:57.690 --> 03:03.390
And one nice thing is that you can get the size of the vector.

03:03.390 --> 03:26.680
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

03:26.680 --> 03:27.010
you want.

03:27.010 --> 03:27.340
Right.

03:27.370 --> 03:35.620
So I have one element that I've added to the vector K and I could even get the capacity

03:38.500 --> 03:45.300
passively of the vector.

03:50.380 --> 03:55.750
So this is how many elements that are actually allocated in memory.

03:55.880 --> 04:08.770
Ok so if I say that it passive vector currently is one capacity your own if we wanted to have more capacity

04:10.760 --> 04:23.190
we could actually reserve it so we can actually say back Reserve and that's reserved 20 k.

04:23.580 --> 04:35.490
So the reserve will actually make the capacity of the vector at least big enough to contain an elements

04:35.580 --> 04:39.130
or in this case 20 it's OK.

04:39.600 --> 04:48.730
So if you're on this we see the capacity has changed to be 20 so there's 20 elements.

04:48.730 --> 04:49.360
OK.

04:49.530 --> 04:57.330
And you might ask OK what happens if I add more more elements then the capacity.

04:57.460 --> 04:57.950
OK.

04:58.170 --> 05:11.540
So I did that say this zero is less than say 20 in this case so they'll be 21 at the end.

05:11.540 --> 05:13.240
This say in at

05:17.740 --> 05:24.340
to push back.

05:24.620 --> 05:36.380
So you're right this so size is 21 and noticed that the capacity change so it changed to 40.

05:36.530 --> 05:41.690
And so it actually doubled the size of the capacity when we went over.

05:41.780 --> 05:53.230
In fact let me do this again and see it change at some point.

05:53.380 --> 05:53.810
OK.

05:53.810 --> 05:55.120
So let's just come at this.

05:55.120 --> 05:55.870
Ouch.

05:56.020 --> 06:03.820
So each time we add We'll check to see the size and to see if the capacity change through this

06:07.120 --> 06:08.630
clips do it.

06:09.020 --> 06:09.580
OK.

06:09.580 --> 06:21.030
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.

06:21.050 --> 06:21.290
Right.

06:21.310 --> 06:33.370
So once we added the twenty first element then the capacity changed K so it regrew the entire internal

06:33.370 --> 06:38.550
array that it uses to store all the integers.

06:39.150 --> 06:43.610
So that's that's really what a vector is at its heart.

06:43.630 --> 06:51.220
So it's dynamically changing array and then growing array.

06:51.520 --> 07:00.590
We can also say remove something from the vector say Interac.

07:01.060 --> 07:04.910
Pop back and doesn't take any arguments.

07:04.940 --> 07:11.400
And this will actually remove the last element

07:14.570 --> 07:15.440
in the vector

07:18.250 --> 07:23.780
so if we said we just comment this

07:27.340 --> 07:29.530
we should have 20.

07:29.650 --> 07:34.500
It's the sizes 20.

07:34.720 --> 07:40.850
And those of the capacity didn't change because it already knows that we can have more than that right.

07:40.870 --> 07:42.780
In our programs of our.

07:43.210 --> 07:45.590
And of course we can check.

07:45.700 --> 07:53.910
Well we could actually before we do that we could actually remove all the elements in the vector.

07:54.070 --> 07:56.910
So it's a Interac not clear.

07:59.010 --> 08:13.870
So this will remove all the elements in the vector gay and we can also check to see if the vector is

08:13.870 --> 08:14.730
empty.

08:18.000 --> 08:20.180
This or turn true if it's empty.

08:20.200 --> 08:21.650
So we say

08:24.600 --> 08:26.330
actor is empty

08:31.160 --> 08:32.070
else.

08:35.240 --> 08:42.350
Or.

08:42.390 --> 08:45.630
So this should only print that the is empty at the act.

08:45.710 --> 08:52.060
So it's an and after the clear we see that the vector is empty.

08:52.060 --> 08:53.770
So the size is zero.

08:53.780 --> 09:09.470
In this case and one last thing just like arrays we can actually set individual elements of the vector

09:09.890 --> 09:13.300
using this indexing operator.

09:13.430 --> 09:15.160
And just like arrays.

09:15.200 --> 09:18.820
So I say 0 is 5.

09:18.860 --> 09:21.960
Say and I can say See.

09:21.990 --> 09:23.190
Ouch.

09:23.320 --> 09:24.910
Ouch.

09:25.890 --> 09:28.580
0 is

09:33.420 --> 09:38.690
x.

09:39.120 --> 09:42.710
This.

09:42.900 --> 09:47.580
You see that act of 0 5.

09:47.860 --> 09:57.800
So you can access individual elements just like arrays.

09:57.860 --> 10:02.350
These will be the main functions we'll use in our projects.

10:02.630 --> 10:08.210
So I show you a few more that will be needing in our mini project.

10:08.210 --> 10:20.010
And if you want to see what else the vector type can do you can actually check out this link here.

10:24.020 --> 10:28.860
And you can see all the other functions that the vector has.

10:28.880 --> 10:37.010
Generally speaking vectors are far safer than using arrays and especially dynamically allocated trees

10:37.430 --> 10:45.890
and they're more convenient since since a lot of the work will be taken care of for you.

10:46.190 --> 10:49.170
For example resizing your array.

10:49.220 --> 10:53.390
One last thing is they're use with functions.

10:53.390 --> 11:02.970
So just like strings you want to pass a vector by reference k

11:10.080 --> 11:10.430
k.

11:10.440 --> 11:16.770
So if you want to add to it or change it in any way you should always be passing it by reference.

11:16.770 --> 11:24.850
Otherwise if you pass it by value it will do that kind of very expensive copy that we saw with strings.

11:24.910 --> 11:32.760
Or again if you want to not be able to change it you should pass it by constant reference so that's

11:32.820 --> 11:33.850
really yeah.

11:33.900 --> 11:42.690
And we'll be seeing some more examples of vectors and strings in the upcoming finale of The Mindy Project

11:43.140 --> 11:49.920
and and also in the game Space Invaders clone that we're right.

11:50.000 --> 11:56.410
And so I want to talk about sorting vectors in the next lecture.
