In this video, we'll learn about the STL map containers.
These are also associative containers.
They're collections of stored objects that allow for
fast retrieval using a key.
The STL provides both sets and maps.
We saw sets in the last video.
In this video, we'll learn about maps.
The STL has four types of map containers, the map, the
unordered-map, the multimap and the unordered-multimap.
We'll focus mainly on the map since it's the one that's most often used.
In order to use the map, you must include the map header file.
The map is like a dictionary where we have a key, a word in the case
of a dictionary and an associated value, which would be its definition.
Maps store their elements as key value pairs using the stood pair objects.
The elements in the map are ordered by key, and no duplicate keys are allowed.
We can directly access an element in the map using that key and
all iterators are available.
Let's initialize a few map objects.
In the first example in this slide, I declare m1 as a map of
string-integer, key-value pairs.
The key is the string and the value is the integer.
Then I initialize m1 with two key value pairs, Larry, 18 and“ Moe, 25.
In the second example, I declare m2 as a map of string-string key
value pairs, and I'm initializing it to Bob, Butcher, Anne, Baker,
George, Candlestick maker.
We'll use the map m in this slide for the next several examples.
The size method returns the number of elements that are in the map,
3, in this case, and max-size will tell us the maximum number
of elements the map can contain.
Maps have no concept of front or back.
We'll see how we can add elements in the next slide.
So, let's see how we can insert elements into a map.
We can use the insert method.
Remember, maps consist of stood pair objects where the first
attribute is the key and the second attribute is the value.
We can create p1 as a stood pair object of type string-string and
initialize it to James Mechanic.
James is the key, mechanic is the value.
We can then insert p1 into the map m, since m is a map of string-string
key value pairs, pretty easy!
We can also use the stood make-pair function to make it even easier for
us to create those key value pairs.
And we can insert them right into the map.
You can see in the last example how easy it is.
In this case, we're adding the key value pair, Roger, Ranger to the map.
So, let's see another way, we can insert elements into the map.
We can use the subscript operator and the dot-at method
to make the syntax pretty easy.
Suppose I want to insert the Frank Teacher, key value pair into the map m.
I can provide the key Frank in this case inside the square brackets
and assign the value to it.
This will insert the key value pair into the map m if it doesn't exist.
I can also modify existing map values in the same way
and the syntax is dead simple.
If I wanted to change the value associated with the key Frank from
teacher to instructor, I can simply use the subscript operator to do it.
The dot-at method does the same thing with different syntax of
course but it will throw an exception if the key is not in the map.
We can also erase elements in a map very easily m.erase and
we’ll erase the key value pair whose key is Anne from the map.
We can also use the find method to get an iterator to a maps element.
As with the set for efficiency, use the member method, find, not
the find in the algorithm library.
In the second example, I'm finding the key, Bob, in the map.
If I get back an iterator, that's not pointing to the end of the map.
Then I found the matching element.
If I wanted to erase that element, I can simply call
m.erase and pass in the iterator.
In the last example, that's exactly what I'm doing.
I'm looking for the key George in the map, and if
it's in there, I'm erasing it.
Finally, we can use the count method the same way we did with sets.
It's an easy way to see if an element exists in the map.
If it does exist, count will return 1, and if it doesn't, count will return 0.
As we've seen with all the STL containers, the clear method removes
all elements from the map, and the empty method returns true if the
map is empty, faults otherwise.
Well, that's a quick summary of the map class.
As I mentioned previously with all the STL components,
there's much more to learn.
Please, become familiar with the basics and then take your
learning to the next level by studying the STL documentation.
Let's see some of the other map containers provided by the STL.
The multimap class is also defined in the map header file.
A multi-map is a map that's ordered by key but does allow duplicate elements.
All iterators are available to the multimap.
The unordered-map is in the unordered map header file.
The unordered-map is a map that's unordered.
Unordered-maps do not allow reverse iterators.
The last type of map provided by the STL is the unordered-multimap.
The unordered multi-map does not order elements and it does allow duplicates,
again no reverse iterators are allowed.
These maps are very powerful and pretty easy to use.
Let's head over to the IDE, and we'll see some examples of using the map.
Okay, so I'm back in the IDE.
I'm in the section 20 workspace in the map project.
And what I'd like to do is go over a couple of map examples, and one in
particular that I think is going to help you out a lot with the challenge
that's coming up in the next video.
So, let's start with test one, and there's only, I think,
two tests in this example.
Test one's a little long but we'll go through it slowly.
And then what we'll do is we'll talk about display function I wrote.
Okay, so we're here in test one, and what I've got here is
I've defined m to be a map of string, integer key value pairs.
The string is the key and the integer is the value.
These guys can be just about anything.
I'm just going to use strings for the key because they're really easy to
understand, but those keys can actually be our own custom objects as well.
And the value can be more complicated as well.
What we're going to do is, in the next test, what we're going to do is we're
going to have the key be a string and the value be a set of integers.
So, again you can build these things up to get really complicated
and very, very, very powerful.
So, again m is a map of string, integer key value pairs.
Here are my pairs right here.
These are not person objects, I don't even have a person class in this demo.
So, there's the string, Larry and there's the value in the
integer 3, Moe 1, Curly 2.
What this means?
Who knows?
You're just associating a string with an integer, it could be how many
times Larry went to the movies today.
It could be just about anything, okay.
Then I'm displaying m, and you can see that right up here.
It's displaying Curly 2, Larry 3, Moe 1.
Now look at the ordering there, it's ordering by key, and the
key in this case is the string.
So, you’re going to get Curly, first, Larry, second, Moe third.
It's using string order, perfect!
Now let's add Anna, 10 to that map m.
Remember what we're doing is we're adding stood pairs.
So in this case, we're creating a stood pair object right here, and it's got
to be of the same key value pair types.
It's got to be string.
Int, it can't be anything else.
Or I can't insert it into this map here.
So, in this case, there's my key, there's my value, I'm creating a pair.
And I'm inserting that pair into m and then I'm displaying m.
So, when you see that next display statement here, you can see
Anna is in there now with a 10.
And Anna is at the beginning of the list because it's
being sorted by the key.
Now there's a handy little function called make-pair
that makes this really easy.
So, we can insert, call make-pair, it's going to create that stood
pair for us, and it's going to be, the key is Joe the value is 5.
So, that'll automatically do kind of what we did up here without all the
template parameters, or any of that.
It'll figure it all out.
So, in this case, it's going to make the pair Joe 5 and insert that
into the map, and then we're going to display the map right here.
And you can see that Joe 5 is now in the map, pretty easy, right!
Now here, this is really easy here.
In this case, I'm saying m, and I'm providing the key.
This is the key inside the subscripts, so what it's going to do is it's
going to insert, Frank isn't in there right now, it's going to insert
the key value pair Frank 18 into the map using this syntax, which
is really, really, easy to read.
And then I'm displaying it, and you can see right here that there is Frank 18.
Now what's really cool is if I wanted to change the value associated with
the key Frank, right now it's 18.
Suppose I wanted to add 10 to it, all I have to say is give
me the value for that key in the map, that's what I'm doing here.
And add 10 to it.
Display it again.
So, now you can see that Frank is 28 right here, right!
So, I've added 10 to the 18.
Pretty cool!
Let me clear this and scroll up just a little bit, do a couple more examples.
Let's do this one right here.
All I want to do here is erase Frank from the map, I'm just
providing the key right here.
Remember, this is all key based, so in this case, m.erase Frank, and
then I'm displaying the map again.
You can see the output right here on this line, Frank is gone.
Frank was here, it was 28, and now it's gone.
And then we can call the count methods here just, so you see how those run.
I'm displaying the count for Joe, how many times does joe appear in the map?
Well, it's either there, or it's not there.
In this case, Joe is there, so we're getting back a 1 and Frank
is no longer there, because I just deleted Frank right here.
So, I should get back a 0 and that's exactly what I get back right here.
And then what we can do is we can call the find method, remember
this is defined method for the map class not these the find
method in the algorithm’s library.
So, I want to find Larry in this map m.
I'm going to get back an iterator, that iterator is an iterator not to
Larry, not to Larry's value which is 3 but to the stood pair, so if
I get back an iterator and I'm not at the end, then I've got a pair.
So, I can access the first element in the pair and the second
element of the pair, makes sense.
The first element is the key the second element is the value.
So, this right here will display Larry, this right here will display
3, and that's what's happening right here, found Larry 3.
Then of course we're clearing the map and displaying the map.
And now we've got an empty map right here.
Okay, so go through these slowly, you can play them with your own, you can,
you know modify this and learn from it.
Let's take a look at test two, and we're here in test two, you can
see the output actually, let me scroll that up as well, perfect.
You can see that test two's output is right over here
on the right in the console.
Now this one's a little bit more complicated, so let's
go through this slowly.
I've got a map, it's called grades.
And it's a map of string is the key and the value is a set of integers.
So, in this case, grades is a map, the key is a string, and
the value is a set of integers.
So, I can have values in there like Frank is the key, and I could have a
set of integers 10, 20, 30, let's say.
Makes sense!
So, this is pretty cool.
So, in this case, let's say that I'm trying to model the top three
grades that Frank got this semester.
Remember we can't have duplicates in here, so we're
just having the three grades.
So, what we're doing is we're saying the key is Frank, right!
That way I can identify that that key value pair, once I have that,
I can get the value part of it.
It's a set of integers and then I can process the set of integers, so
that's what we're doing here, right.
So, let's go through this again.
Here is key value, here's the key, here's the key, here's
the key, there's the value.
It's a set of two integers.
Here's a set of one integer.
Here's a set of three integers.
And then what I'm doing is I'm just displaying grades.
And I'll show you how that display function works in a minute, but you can
see I'm displaying grades right here.
And it's ordering rather by the key which is a string.
So, Curly 80, 90, 100, there it is.
Larry 90, 100, and Moe .
Now suppose I want to insert 95 into that set of integers for Larry.
I can get Larry right here, that's going to return a
reference to that element, and I can insert 95 into its value.
Well, remember, its value is a set, so I'm actually inserting 95 into that set
of integers, so now you can see that Larry originally was 100 and 90, now
Larry is 90, 95, and a 100 right here.
Right, so I just added that 95 right in here.
And remember that set of integers is ordered as well,
so we get that benefit as well.
So, we've got the map is ordered, and the set within
the map is ordered as well.
Alright. so, my last example here is let's say that we want to find Moe.
So, let's look for Moe.
We're going to get back in iterator, right.
If that iterator is not at the end, then I found
Moe, well, Moe is in there.
I know he's in there, he's right over here.
So, now what I want to do is using the iterator, I want to access
the second attribute, right.
Remember the first is the key, I know what the key is, it's Moe.
What I want is the second attribute which is the set of integers, and I
want to insert a thousand in there.
So, it's similar to this example right here except that in this
case, we're using an iterator.
So, I'm going to insert a thousand into the set of
integers associated with Moe.
Remember Moe is the key, the set of integers is the value.
And you can see right there is the 1000.
And that's where I'm displaying the grades.
Now let me show you the display function.
There's actually two display functions I've got.
This one right here which is a template function that's
just displaying any two types.
This is the one that's really being called in this case.
So again, in this case, I might be getting my, it's a map right.
It's a map of key value pairs.
The key is this guy right here that could be Curly.
And the value is the set of integers associated with Curly, so that could be
the set 80, 90, and 100 in this case.
So, that's the information I'm getting, what I have to do is
display it out nicely like this.
So, the first thing I'm doing, I'm just displaying that little the left
bracket which is this guy right here.
Then for each element, each element, I called it in that map.
The first item is the key, so that's Curly, that's Larry, that's Moe.
And then each time I get one of those what I'm doing is here
I'm iterating over the second which is the set of integers.
Makes sense!
So, remember the outer loop here is iterating over Curly, Larry, Moe.
And then each time I have one of those I'm iterating over their
values, the set right here.
So, that's just displaying that information nicely.
I know this may be a little confusing if you sit down and walk through
it, it'll be really clear I hope.
In the next video, we're going to do a challenge where we're
going to use something that's almost exactly like this.
We're going to use a map where the string is the key and we have
a set of integers as the value.
And what we're going to do is we're going to grab words from a
document, and we're going to figure out how many times they occur.
And then we're going to figure out how many times, what lines they occur on.
So, it's going to be pretty fun.
So, I'll see you in the next video.