WEBVTT

00:00.120 --> 00:07.800
Hello again! In this video, we are going to look at nested maps. A nested map is a map which contains another

00:07.800 --> 00:09.840
map, usually as its value.

00:10.740 --> 00:15.360
As a very simple example, we are going to look at the structure of an adventure game.

00:15.840 --> 00:21.300
So this has a series of levels, and the player has a different adventure in each level.

00:22.740 --> 00:26.100
We are going to use a map to represent the objects in each level.

00:26.550 --> 00:32.730
So in the first level, we have a player at position 1, and a door at position 10. So the player

00:32.730 --> 00:33.930
is just looking at the door.

00:35.640 --> 00:41.370
In the second level, we have the player at position 5, and the monster at position 10. So

00:41.370 --> 00:46.980
the player has walked towards the door and the door opens and there's a monster standing there. (Help!)

00:48.750 --> 00:53.940
So if we were writing the complete game, which I am not going to do, then it would probably have quite

00:53.940 --> 00:56.730
a lot of these levels. And we need some way to organize them.

00:57.300 --> 00:59.460
So we are going to create a game map.

01:00.000 --> 01:05.910
This will have the number of the level as its key and then the map for that level

01:06.210 --> 01:06.870
as the value.

01:07.260 --> 01:14.070
So we have level one with the level one map, and so on. And the value of this is going to be a map with

01:14.550 --> 01:17.550
keys which are ints, and values which are strings.

01:18.660 --> 01:21.870
If we were doing this in older C++, we would have to put a space in there.

01:22.590 --> 01:26.010
Otherwise, the compiler gets confused. But we no longer need to do that.

01:26.580 --> 01:33.030
If we write a lot of code which manipulates this game map type, then we can end up with a lot of angle brackets.

01:33.570 --> 01:40.640
So good thing to do is to create a type alias. And we can do that with a "using" statement, or a 

01:40.650 --> 01:40.920
typedef.

01:41.400 --> 01:47.700
So when we put level_map, the compiler will know that we actually mean a map with int keys and string

01:47.700 --> 01:48.180
values.

01:49.470 --> 01:51.750
And this makes the code for the game map a bit simpler.

01:53.610 --> 01:59.790
So what happens if we intervene over this game map? We will get the elements of this game map,

01:59.790 --> 02:08.520
which are pairs, with the level number as the key and the level map as the value, so we can then iterate

02:08.520 --> 02:10.560
over all the elements in the level map.

02:10.920 --> 02:13.380
And if we keep doing that, we will get all the elements in the game.

02:15.060 --> 02:19.090
So our loop will get the level data for each level.

02:19.560 --> 02:21.330
The first member is the number of the level.

02:21.690 --> 02:25.710
And then, if we iterate over the second member, we get all the objects in that level.

02:27.550 --> 02:31.270
If we have C++17, then the code is a bit easier to read.

02:31.990 --> 02:35.200
We iterate over the game maps, and we get the level number and the level map.

02:35.620 --> 02:39.580
Then we iterate over the level map, and we get the positions and the objects.

02:42.990 --> 02:46.320
And there we are, we have our two levels and the objects within those levels.

02:47.190 --> 02:53.340
If we want to add an object to a level, or even add a completely new level, then we can just call the

02:53.460 --> 02:54.540
insert() member function.

02:55.830 --> 03:02.670
I have moved the loop iteration which prints out the elements into a separate function, to shorten the code a bit.

03:03.810 --> 03:09.150
Then we create and populate our map, as we did before. We call this function to print it out.

03:10.050 --> 03:13.320
And then we are going to insert a new object into level two.

03:13.590 --> 03:17.880
So we need to get an iterator to this element, in the game map.

03:18.720 --> 03:19.620
So we call find().

03:20.820 --> 03:23.460
And then the second member will be the level two map.

03:23.730 --> 03:29.880
So if we insert this element into the second member, that will add the element to the level two.

03:30.930 --> 03:34.920
So we now have a magic wand at position three, which is just behind us.

03:34.980 --> 03:39.380
If you are keeping track of this. So the player is at 5, the wand is at 3, the monster is at 10.

03:39.720 --> 03:42.000
So you have to look behind to get the magic wand.

03:45.200 --> 03:50.900
And then to insert a new level, we just create a level map for it, and then we insert it with the

03:50.900 --> 03:52.010
appropriate level number.

03:57.060 --> 04:04.170
So there is our initial map. Then we insert the magic wand into level two, at position 3.

04:05.580 --> 04:08.640
And then we insert our new level, at level 3.

04:13.450 --> 04:19.000
And for removing objects we can just call erase(). So, same code again.

04:22.280 --> 04:25.910
And now we are going to remove the element with key 10 from level 2.

04:26.150 --> 04:27.710
So we are going to remove the monster.

04:28.730 --> 04:33.770
So we need an iterator to the level two entry in the game map.

04:35.130 --> 04:36.960
But we already have one from earlier on.

04:37.120 --> 04:38.100
So we can just use that.

04:39.350 --> 04:43.640
We need to use reference to auto, because we are going to modify this level 2 map.

04:44.730 --> 04:49.050
Then we call find() to get the element in level two, which has the key 10.

04:50.010 --> 04:51.120
And then we just erase it.

04:54.500 --> 04:59.030
So we have the maps that we had from the last time we ran this program, with the magic wand, and

04:59.030 --> 05:02.420
the level 3, and then we remove the monster from level two.

05:03.050 --> 05:05.090
So we have waved the magic wand at the monster.

05:05.090 --> 05:07.890
The monster has disappeared, and now we are right next to a bomb.

05:08.450 --> 05:09.710
(This game just keeps getting worse!)

05:10.430 --> 05:11.810
Okay, so that is it for this video.

05:12.170 --> 05:12.950
I will see you next time.

05:13.220 --> 05:15.020
But until then, keep coding!
