Hello again! In this video, we are going to look at nested maps. A nested map is a map which contains another
map, usually as its value.
As a very simple example, we are going to look at the structure of an adventure game.
So this has a series of levels, and the player has a different adventure in each level.
We are going to use a map to represent the objects in each level.
So in the first level, we have a player at position 1, and a door at position 10. So the player
is just looking at the door.
In the second level, we have the player at position 5, and the monster at position 10. So
the player has walked towards the door and the door opens and there's a monster standing there. (Help!)
So if we were writing the complete game, which I am not going to do, then it would probably have quite
a lot of these levels. And we need some way to organize them.
So we are going to create a game map.
This will have the number of the level as its key and then the map for that level
as the value.
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
keys which are ints, and values which are strings.
If we were doing this in older C++, we would have to put a space in there.
Otherwise, the compiler gets confused. But we no longer need to do that.
If we write a lot of code which manipulates this game map type, then we can end up with a lot of angle brackets.
So good thing to do is to create a type alias. And we can do that with a "using" statement, or a
typedef.
So when we put level_map, the compiler will know that we actually mean a map with int keys and string
values.
And this makes the code for the game map a bit simpler.
So what happens if we intervene over this game map? We will get the elements of this game map,
which are pairs, with the level number as the key and the level map as the value, so we can then iterate
over all the elements in the level map.
And if we keep doing that, we will get all the elements in the game.
So our loop will get the level data for each level.
The first member is the number of the level.
And then, if we iterate over the second member, we get all the objects in that level.
If we have C++17, then the code is a bit easier to read.
We iterate over the game maps, and we get the level number and the level map.
Then we iterate over the level map, and we get the positions and the objects.
And there we are, we have our two levels and the objects within those levels.
If we want to add an object to a level, or even add a completely new level, then we can just call the
insert() member function.
I have moved the loop iteration which prints out the elements into a separate function, to shorten the code a bit.
Then we create and populate our map, as we did before. We call this function to print it out.
And then we are going to insert a new object into level two.
So we need to get an iterator to this element, in the game map.
So we call find().
And then the second member will be the level two map.
So if we insert this element into the second member, that will add the element to the level two.
So we now have a magic wand at position three, which is just behind us.
If you are keeping track of this. So the player is at 5, the wand is at 3, the monster is at 10.
So you have to look behind to get the magic wand.
And then to insert a new level, we just create a level map for it, and then we insert it with the
appropriate level number.
So there is our initial map. Then we insert the magic wand into level two, at position 3.
And then we insert our new level, at level 3.
And for removing objects we can just call erase(). So, same code again.
And now we are going to remove the element with key 10 from level 2.
So we are going to remove the monster.
So we need an iterator to the level two entry in the game map.
But we already have one from earlier on.
So we can just use that.
We need to use reference to auto, because we are going to modify this level 2 map.
Then we call find() to get the element in level two, which has the key 10.
And then we just erase it.
So we have the maps that we had from the last time we ran this program, with the magic wand, and
the level 3, and then we remove the monster from level two.
So we have waved the magic wand at the monster.
The monster has disappeared, and now we are right next to a bomb.
(This game just keeps getting worse!)
Okay, so that is it for this video.
I will see you next time.
But until then, keep coding!