WEBVTT

00:00.120 --> 00:03.720
Hello again! In this video, we are going to look at maps and insertion.

00:04.650 --> 00:10.800
The reason for this is that there are some new features in C++ 17 for working with maps, and we need

00:10.800 --> 00:12.810
a bit more background before we can go onto those.

00:14.630 --> 00:18.890
So just to recap, we know two ways of inserting an element into a map.

00:19.580 --> 00:27.500
These are the subscript operator and the insert() member function. Looking first at the subscript operator.

00:27.800 --> 00:30.950
this provides so-called "insert or assign" functionality.

00:31.550 --> 00:38.060
So if there is no element in the map which has the key we are using, this will insert a new element. If

00:38.060 --> 00:40.100
there already is an element with that key,

00:40.340 --> 00:42.410
it will assign to the value of that element.

00:46.200 --> 00:51.540
So if we have some code like this: we create a map object, and then later on, we have a statement

00:51.540 --> 00:52.050
like this.

00:52.920 --> 00:59.160
There are two possible outcomes depending on what happens in here. Either there is no "Graham" element,

00:59.160 --> 01:04.530
in which case we can insert one, or there already is a "Graham" element, in which case we overwrite it.

01:05.640 --> 01:10.260
So sometimes we might want to know whether we have actually created a new element or not.

01:10.770 --> 01:13.200
And unfortunately, there is no way to know.

01:18.350 --> 01:24.080
Looking at the actual way that the subscript operator inserts an element, when it does insert one.

01:24.470 --> 01:25.880
This is done in two stages.

01:26.330 --> 01:28.730
So first, it will create the new element.

01:29.570 --> 01:35.300
The key is going to be the argument to the subscript, and the value is going to be default-initialized.

01:36.440 --> 01:42.440
So this means that the value type has to be either a built in type or something that has a default constructor.

01:43.100 --> 01:45.470
And that can be an irritating restriction sometimes.

01:46.790 --> 01:51.170
Also, if an exception gets thrown, probably by the assignment.

01:52.210 --> 01:54.230
We have not actually covered exceptions yet,

01:54.250 --> 01:59.950
I know. But, basically, what happens is that this operation will end before it has finished doing all its

01:59.950 --> 02:00.280
work.

02:00.760 --> 02:04.210
And in that case, we end up with a partially populated element.

02:04.570 --> 02:05.440
So it has a key, but

02:05.440 --> 02:07.510
the value is default-initialized.

02:09.300 --> 02:14.970
Also, if we forget to do the assignment part, then we end up with a partially populated element as

02:14.970 --> 02:15.270
well.

02:15.690 --> 02:21.180
So this will have the key "Graham" and an undefined value, or possibly default-initialized.

02:23.450 --> 02:29.210
And the return value from the operation is a reference to the value member of the element, so that tells

02:29.210 --> 02:30.560
us nothing about what happened.

02:33.770 --> 02:39.110
The insert member will only insert a new element - at least it will only directly insert one. If you want

02:39.140 --> 02:41.060
to do anything else, you have to write a bit more code.

02:42.440 --> 02:44.640
If the map already has an element with the same key,

02:44.670 --> 02:45.410
nothing happens.

02:46.250 --> 02:51.410
And the return value from insert() will actually tell us whether we actually inserted an element or not.

02:52.760 --> 02:57.020
There is no requirement that the type of the value has a default constructor.

02:57.740 --> 03:03.140
And if an exception is thrown, then the insertion has no effect. So the operation does get rolled

03:03.140 --> 03:03.440
back.

03:05.240 --> 03:10.790
So the insert() member function has some advantages over subscripts, but it does not perform "insert

03:10.790 --> 03:11.420
or assign".

03:11.780 --> 03:13.790
If we want that, we have to write code like this.

03:14.360 --> 03:19.760
So we do the insert. Then we get the return value. Then we check the second member.

03:20.600 --> 03:23.330
And if that is false, then we need to do the assignment.

03:24.140 --> 03:29.330
So it is nothing very complicated, but it is five statements for something which really is a single logical

03:29.330 --> 03:29.930
operation.

03:32.520 --> 03:35.040
So let's try this out with the map that we had before.

03:37.320 --> 03:46.870
We call insert(). And then, if the insert() does not succeed, we need to assign to the value member of this

03:47.070 --> 03:47.600
element.

03:49.380 --> 03:52.560
So there it is, we already had an element with the key "Graham".

03:52.920 --> 03:55.620
So we needed to modify the value.

03:56.950 --> 03:59.170
OK, so that is it for this video.

03:59.620 --> 04:02.680
I will see you next time, but until then, keep coding!
