WEBVTT

00:00.210 --> 00:03.510
Hello again! In this video, we are going to look at the C++ map.

00:04.350 --> 00:08.160
This is another associative container. In the set,

00:08.520 --> 00:13.410
the elements just consisted of the key and nothing else. In a map,

00:13.530 --> 00:15.150
the elements actually have two parts.

00:15.810 --> 00:18.750
There is the key, and a so-called "value".

00:19.530 --> 00:22.540
These are stored as a pair object.

00:23.040 --> 00:24.990
And the first member of the pair is the key,

00:25.440 --> 00:28.170
and the second member is the value of the element.

00:29.760 --> 00:31.500
Again, the keys have to be unique.

00:32.070 --> 00:36.840
We cannot have two elements with the same key, but we can have two elements which have the same value,

00:37.200 --> 00:37.650
provided

00:37.650 --> 00:38.700
the keys are different.

00:39.150 --> 00:43.980
And again, they are stored in a tree, in order, using the less-than operator of the key.

00:47.610 --> 00:54.600
The value of the pair is used to store the data for that element, and the key is used to locate it.

00:55.230 --> 00:59.400
Normally there is some kind of relationship between the key and the value.

01:00.870 --> 01:07.760
For example, if we have a map of the company's staff, we could have the employee number as the key

01:08.250 --> 01:10.200
and their personnel record as the data.

01:11.370 --> 01:17.370
If we have a map of results from a sporting event, we could have the name of the competitor as the

01:17.370 --> 01:21.450
key, and their result in the event as the value.

01:22.830 --> 01:26.080
When we use a map, we search for a particular key.

01:26.100 --> 01:31.980
So one of these. And then we get an element, and we look up the data, from the value member of the element.

01:32.580 --> 01:34.800
So the key acts as an index for the map.

01:35.190 --> 01:39.090
This is very similar to a "hash map" or a "directory" in other languages.

01:43.920 --> 01:45.660
The structure will look like this.

01:45.990 --> 01:49.590
We have two parts to the element, the key and the value.

01:50.160 --> 01:58.170
So here we have the key, which is presumably a job level, or a pay grade, and the value is a job description.

01:58.890 --> 02:01.680
So these are all organized depending on the key.

02:02.820 --> 02:04.470
The value is not taken into account.

02:04.680 --> 02:07.860
So "Driver" is greater than "Analyst", but that does not matter.

02:08.900 --> 02:11.540
All that matters is that 42 is less than 67.

02:14.130 --> 02:20.640
As with set, we can use insert() and erase() for adding and removing elements. The element type is

02:20.640 --> 02:24.660
a pair, so we need to provide a pair object as the argument to insert.

02:25.530 --> 02:27.840
And typically we use make_pair().

02:28.080 --> 02:31.770
The first argument to make_pair() is going to be the key of our new element.

02:32.250 --> 02:36.120
And the second member is going to be its value. In C++11,

02:36.540 --> 02:41.640
we can also use list initializers, so we can put the key and value inside a pair of braces.

02:42.930 --> 02:44.660
And again, the keys have to be unique.

02:44.670 --> 02:50.760
So if we try to insert an element, and there is already an element in the map which has the same key,

02:51.120 --> 02:57.900
then this will fail. And we check for that exactly the same way as we did in the set: we get a paper

02:57.900 --> 03:02.100
object returned. The second member is a bool, which will tell us if it succeeded.

03:03.660 --> 03:05.670
So we include the <map> header.

03:06.720 --> 03:08.610
We create an empty map object.

03:09.900 --> 03:13.560
This is going to have a key of type string and a value of type int.

03:14.790 --> 03:16.590
And then we add some elements to it.

03:17.070 --> 03:19.710
We first call make_pair(), to show how to do that.

03:20.280 --> 03:27.870
So this is going to create a new element, with key "Maybelline" and value 86. And then using the list

03:27.870 --> 03:27.990
initializer.

03:28.380 --> 03:31.020
This is going to have the key "Graham" and the value 78.

03:31.530 --> 03:33.330
And then we can use this function to print them out.

03:33.330 --> 03:38.520
It takes a map by reference to const and then we have a range for loop.

03:39.480 --> 03:41.610
So, all pretty straightforward, I hope.

03:43.670 --> 03:46.330
And then we have the same logic as we did in the set example.

03:46.700 --> 03:51.020
We try to add another element, which has the same key as an element that is already in the map.

03:52.160 --> 03:55.040
We get a pair object returned from the call to insert().

03:55.550 --> 03:57.950
The second member will tell us if it succeeded.

03:58.640 --> 04:05.120
And the first member will be an iterator to this element, which made it fail, or the new element if it succeeded.

04:06.710 --> 04:12.320
This is iterator is actually going to be an iterator to an object of the element type, which is a pair.

04:13.220 --> 04:15.590
So this is actually going to be a pair itself.

04:16.280 --> 04:19.370
And the first member is going to be the key of this element.

04:19.970 --> 04:22.640
And the second member is going to be the value of the element.

04:25.160 --> 04:27.920
Then we remove the element which has this key.

04:28.250 --> 04:31.700
And then we try to insert it again. And this time it should succeed.

04:34.720 --> 04:36.700
So there is our initial map elements.

04:37.030 --> 04:42.970
They are in alphabetical order, the order of the keys. "Graham" comes before "Maybelline". Then we try

04:42.970 --> 04:46.180
to add another element with key "Graham". The map already has one.

04:46.750 --> 04:48.340
So the map is not modified.

04:49.240 --> 04:54.190
Then we remove the element with key "Graham", and then we try to add our element again.

04:54.490 --> 04:58.630
So this time we have a new element, with key "Graham" and score 66.

05:03.660 --> 05:06.300
Map has a feature which the set does not.

05:06.660 --> 05:09.270
And that is subscript access to elements.

05:09.750 --> 05:13.960
However, it does work differently from the way that we are used to, with strings, vectors and arrays.

05:16.200 --> 05:21.240
So we put the name of the map, and then square brackets. And this will finish the element, which

05:21.240 --> 05:26.430
has this argument as its key, and it will return the value of that element.

05:26.820 --> 05:33.560
So this will return the value of the element with key "Graham". If we assign to this, then this will update

05:33.570 --> 05:35.150
the value of that element.

05:35.610 --> 05:37.140
So Graham now has the score

05:37.140 --> 05:37.830
66.

05:40.390 --> 05:44.890
However, if we put something in here, and there are no elements in the map which already have

05:44.890 --> 05:51.430
that argument as key, then this will create a new element, and this will be the key of that element.

05:51.850 --> 05:57.010
So this will now create a new element with key "Grace", and set its value to 66.

05:58.760 --> 06:04.190
So if the element does not already exist, it will be created. If the elements does exist,

06:04.460 --> 06:07.310
it willl overwrite the existing value of the element.

06:10.750 --> 06:12.100
So let's try this out.

06:12.400 --> 06:18.190
So we are starting off with the same map as we had before, with the same initial elements. Then we use

06:18.520 --> 06:21.730
subscripts, to access Graham's value.

06:22.600 --> 06:29.290
Then we set Graham's value to 66, and then we see what happens when we search Grace's score to 66.

06:32.720 --> 06:34.460
So those are the original elements.

06:35.690 --> 06:37.850
We get Graham's score, which is 78.

06:38.420 --> 06:40.720
Then we set Graham's score to 66.

06:40.730 --> 06:41.630
So it has done that.

06:42.410 --> 06:44.960
And then we add Grace with a score of 66.

06:45.530 --> 06:47.990
So we now have this new element for Greece.

06:48.590 --> 06:52.730
And you will see that the elements are still in alphabetical order.

06:53.450 --> 06:54.320
There are still ordered.

06:54.650 --> 06:58.730
And also that we can have two elements with the same value and different keys.

07:02.630 --> 07:06.500
We can call the find() and count() member functions, as we did with the set.

07:07.130 --> 07:14.540
So here we call find() to get Graham's score, and we call count() to get the number of "Graham"'s in

07:14.540 --> 07:15.290
the map.

07:16.980 --> 07:22.290
We saw with the set that we are not allowed to modify the returned element from the find().

07:22.740 --> 07:24.270
So let's see if we can do this with map.

07:27.030 --> 07:28.500
And no, we cannot!

07:29.220 --> 07:30.320
Lots of compiler errors!

07:31.170 --> 07:33.870
But maybe it is just the key which is the problem?

07:35.400 --> 07:36.900
So let's see if we can change the value.

07:40.520 --> 07:44.840
Yeah, so you can do that, so we can change Graham's scores to 67.

07:45.410 --> 07:49.190
So we found an element with key "Graham", and we set its score to 67.

07:50.090 --> 07:51.530
And there is one element.

07:54.180 --> 07:59.130
So you can modify the value of a map element, but not the key.

08:00.480 --> 08:02.910
And with algorithms, we have the same constraint again.

08:03.660 --> 08:09.870
We can use algorithms provided they only reach each iterators for a map, and they do not try to modify them

08:09.870 --> 08:10.860
or rearrange them.

08:11.970 --> 08:18.790
So we have our call to find_if(). This is going to take an element object as argument.

08:18.820 --> 08:24.220
So this needs to be a pair, of string and int, because that is what the elements of the map are.

08:25.140 --> 08:29.760
And then we have our lambda expression, which will compare the key to "Graham".

08:31.920 --> 08:35.070
And similarly for count_if().

08:37.970 --> 08:40.490
So we get the same results as we did with the member functions.

08:42.540 --> 08:48.870
And maps have the same strengths and weaknesses as sets. They can search or locate an element very quickly.

08:49.560 --> 08:54.870
Inserting and removing elements is usually very fast, unless you are unlucky and the tree has to rebalance.

08:55.740 --> 09:00.240
The main application for maps is for data, which has an index of some kind.

09:00.900 --> 09:06.540
For example, with a contact list, you could have the contact's name as their key, and then the data

09:06.540 --> 09:10.410
would be their phone number or email address or some other data.

09:11.340 --> 09:18.240
The employee records with the employee ID as the key and the record as the value. Results from a database

09:18.240 --> 09:18.650
query.

09:18.930 --> 09:24.690
Quite often, you have a row with an index, so you could use the index as the key and the rest of the

09:24.690 --> 09:26.400
row as the data. And so on.

09:28.150 --> 09:33.130
They are also useful for storing data which naturally falls into key-value pairs.

09:33.400 --> 09:35.290
So things like JSON and XML.

09:36.160 --> 09:37.600
Okay, so that is it for this video.

09:37.960 --> 09:40.750
I will see you next time, but until then, keep coding!
