Hello again! In this video, we are going to look at multiset and multimap. We have already looked
at set and map. In those containers,
the elements have to have distinct keys, so we cannot have different elements which have the same key.
With multiset and multimap.
This requirement is relaxed, so we can have different elements which have the same key.
And apart from that, they all work more or less the same way as set and map. The only real difference is
that multimap does not support subscripting.
So let's try this with a multiset. We still include the
header.
Then we create a multiset object, and we insert some elements. And then we try to insert an element
with the key 6, even though we already have an element with the key 6.
So let's see what happens.
So we call insert three times, with the argument 6, and we end up with three elements with the key
6.
So insert will always succeed.
If you call insert, it will always create a new element, regardless of whether there are any elements
which already have that key.
And the other thing to note is that these are all in order, as you would expect.
And the same with a multimap. We include .
We create an object and add some elements, and then we add some more map elements which have the same
key.
So, "Graham".
And we end up with three elements with the key "Graham". And again, they are all sorted in order of
their keys.
So let's find out what happens if we call erase(). (And the comment rather gives it away!)
So erase() with argument "Graham" will remove all the elements which have key "Graham." And that
may be what we want.
Or perhaps, you may just want to remove one of the elements with key "Graham".
So, a quick recap. insert() will always succeed for multimap and multiset. If we call erase(),
then that will remove all the elements which have that key.
There is a form of erase() which takes a single iterator, so we can use that to remove a single element.
We just have to find an iterator to the element we want to remove.
And how do we do that?
With a map, it is very easy, because only one element can have a given key. With a multimap, there could
be several elements which have the same key. And we need some way to manage all these potential matches.
One thing that does help us, is that the elements in a multimap are sorted.
They are in the order of the keys.
And that means that all the elements which have the same key will be next to each other.
So this means we have a kind of sub-container within the map, which will contain all the elements with
the same key.
And we can use an iterator range to represent those elements.
So there are several ways to do this. One way, which we already know about, is to use find() and count().
If we call find() for a multimap, this will return an iterator to the first element which has
that key.
And that will be the first element in our sub-range.
If there is no element which matches, then this will return
the last-plus-one iterator, and our range will be empty.
The count() member function will return the number of elements which have that key. And that gives
us the number of elements in our range.
So, now we know enough to loop over all the elements. We can go to the first element and iterate over
every element.
So let's try this with the multimap we had before. We call find() with "Graham" as the key.
Then this will return an iterator to the first element with key "Graham".
And then we can get the number of elements which have key "Graham", and then we can leap over these elements.
So we just keep incrementing the iterator each time.
And there we are.
We get all the elements with key "Graham".
And if we want to find an element with a particular value, then we can just check that inside the
loop, so like that. So,
is there an element with key "Graham" and value 66?
Yes, there is.
Okay, so that is it for this video.
I will see you next time, but until then, keep coding!