WEBVTT

00:00:00.440 --> 00:00:03.470
In the previous lesson, we pushed elements to the back of

00:00:03.470 --> 00:00:06.920
the vector, because that is the easiest thing to do, and

00:00:06.930 --> 00:00:08.750
usually that's all we need.

00:00:09.140 --> 00:00:12.940
But vector also offers us a way to insert elements at

00:00:12.940 --> 00:00:15.160
the specific position in the array.

00:00:15.540 --> 00:00:21.080
I created a new vector and reserved a place for five subjects, and then I filled

00:00:21.080 --> 00:00:26.390
it up by emplacing new subjects. Notice that one of them is a duplicate, and

00:00:26.390 --> 00:00:29.050
this is not a mistake, we will need this for later.

00:00:29.940 --> 00:00:33.530
Now, let's say that I want to insert another element at the

00:00:33.530 --> 00:00:38.440
specific position, maybe at the index of two. To do that, we can

00:00:38.440 --> 00:00:42.340
use the insert function, which takes in the required position and

00:00:42.340 --> 00:00:44.160
the object we want to insert.

00:00:44.640 --> 00:00:48.170
However, this position needs to be defined with an iterator.

00:00:48.180 --> 00:00:51.320
So if the begin iterator is the address of the first element,

00:00:51.330 --> 00:00:57.060
then we can add two to get the address of the element at the index of two,

00:00:57.060 --> 00:01:01.460
and I will create some random subject with the ID of eight.

00:01:02.340 --> 00:01:06.740
This will put the element at that specific position in the array, but this

00:01:06.750 --> 00:01:10.160
insert function works kind of like a push_back function.

00:01:10.540 --> 00:01:14.260
It will move the subject from the main scope to the vector.

00:01:14.640 --> 00:01:17.550
Since we don't want that, let's immediately make this more

00:01:17.550 --> 00:01:21.820
efficient by using the emplace function instead. Emplace

00:01:21.820 --> 00:01:24.960
function to the insert function is what the emplace_back

00:01:24.960 --> 00:01:27.360
function is to the push_back function.

00:01:28.040 --> 00:01:32.610
It will directly instantiate the subject on the vector by passing the right

00:01:32.610 --> 00:01:37.250
parameters. Just like with emplace_back function, you can add these

00:01:37.250 --> 00:01:41.260
constructor parameters as arguments of the emplace function.

00:01:41.940 --> 00:01:44.850
Okay, let's now compile this to see what happens.

00:01:45.540 --> 00:01:50.030
It seems that the emplace function worked, this subject with the ID of

00:01:50.030 --> 00:01:52.860
eight is placed at the third position in the array,

00:01:53.240 --> 00:01:56.860
but the move constructor was invoked five times.

00:01:57.640 --> 00:02:01.270
This is because the old internal array had the capacity of

00:02:01.270 --> 00:02:04.180
five, and once we insert this new element,

00:02:04.190 --> 00:02:09.759
this new array needs to have six elements, so, resizing needs to happen.

00:02:10.440 --> 00:02:14.000
The new array of six elements will first create the new object

00:02:14.000 --> 00:02:18.120
with the parameterized constructor. and it will do so in the

00:02:18.120 --> 00:02:20.450
specified position in the array.

00:02:21.440 --> 00:02:22.200
After that,

00:02:22.210 --> 00:02:26.200
all of the elements from the old array need to be moved to the new array.

00:02:26.420 --> 00:02:29.410
So this is why we see five move constructors,

00:02:29.420 --> 00:02:32.060
because we need to move five elements.

00:02:32.840 --> 00:02:36.720
Of course, if you know that some emplacing will happen in the future,

00:02:36.730 --> 00:02:40.090
you can reserve more memory. In that case, only the

00:02:40.090 --> 00:02:43.920
elements that really have to move will be moved, and no

00:02:43.920 --> 00:02:45.950
new array will have to be created.
