WEBVTT

00:00.180 --> 00:06.090
Hello again! In this video, we are going to look at multiset and multimap. We have already looked

00:06.090 --> 00:08.820
at set and map. In those containers,

00:08.820 --> 00:14.100
the elements have to have distinct keys, so we cannot have different elements which have the same key.

00:14.940 --> 00:16.710
With multiset and multimap.

00:16.710 --> 00:21.180
This requirement is relaxed, so we can have different elements which have the same key.

00:22.350 --> 00:27.900
And apart from that, they all work more or less the same way as set and map. The only real difference is

00:27.900 --> 00:30.420
that multimap does not support subscripting.

00:32.160 --> 00:35.520
So let's try this with a multiset. We still include the <set>

00:35.560 --> 00:35.920
header.

00:37.260 --> 00:44.190
Then we create a multiset object, and we insert some elements. And then we try to insert an element

00:44.190 --> 00:47.910
with the key 6, even though we already have an element with the key 6.

00:49.020 --> 00:50.070
So let's see what happens.

00:51.540 --> 00:57.540
So we call insert three times, with the argument 6, and we end up with three elements with the key

00:57.540 --> 00:57.930
6.

00:58.360 --> 01:00.270
So insert will always succeed.

01:00.720 --> 01:05.820
If you call insert, it will always create a new element, regardless of whether there are any elements

01:05.820 --> 01:06.930
which already have that key.

01:08.270 --> 01:11.990
And the other thing to note is that these are all in order, as you would expect.

01:13.940 --> 01:16.520
And the same with a multimap. We include <map>.

01:17.150 --> 01:22.880
We create an object and add some elements, and then we add some more map elements which have the same

01:22.880 --> 01:23.240
key.

01:23.630 --> 01:24.290
So, "Graham".

01:27.150 --> 01:32.490
And we end up with three elements with the key "Graham". And again, they are all sorted in order of

01:32.490 --> 01:33.150
their keys.

01:35.180 --> 01:41.120
So let's find out what happens if we call erase(). (And the comment rather gives it away!)

01:44.270 --> 01:49.640
So erase() with argument "Graham" will remove all the elements which have key "Graham." And that

01:49.640 --> 01:50.660
may be what we want.

01:50.990 --> 01:54.440
Or perhaps, you may just want to remove one of the elements with key "Graham".

01:56.500 --> 02:02.860
So, a quick recap. insert() will always succeed for multimap and multiset. If we call erase(),

02:02.860 --> 02:05.830
then that will remove all the elements which have that key.

02:06.580 --> 02:13.050
There is a form of erase() which takes a single iterator, so we can use that to remove a single element.

02:13.660 --> 02:17.020
We just have to find an iterator to the element we want to remove.

02:17.710 --> 02:18.910
And how do we do that?

02:21.330 --> 02:28.140
With a map, it is very easy, because only one element can have a given key. With a multimap, there could

02:28.140 --> 02:33.990
be several elements which have the same key. And we need some way to manage all these potential matches.

02:36.710 --> 02:41.060
One thing that does help us, is that the elements in a multimap are sorted.

02:41.300 --> 02:42.740
They are in the order of the keys.

02:43.070 --> 02:46.940
And that means that all the elements which have the same key will be next to each other.

02:47.390 --> 02:51.980
So this means we have a kind of sub-container within the map, which will contain all the elements with

02:51.980 --> 02:52.640
the same key.

02:53.420 --> 02:56.380
And we can use an iterator range to represent those elements.

02:58.170 --> 03:03.630
So there are several ways to do this. One way, which we already know about, is to use find() and count().

03:06.820 --> 03:12.370
If we call find() for a multimap, this will return an iterator to the first element which has

03:12.370 --> 03:12.940
that key.

03:14.580 --> 03:16.710
And that will be the first element in our sub-range.

03:17.400 --> 03:19.980
If there is no element which matches, then this will return

03:19.980 --> 03:23.220
the last-plus-one iterator, and our range will be empty.

03:24.340 --> 03:29.270
The count() member function will return the number of elements which have that key. And that gives

03:29.270 --> 03:31.030
us the number of elements in our range.

03:31.570 --> 03:37.210
So, now we know enough to loop over all the elements. We can go to the first element and iterate over

03:37.210 --> 03:37.930
every element.

03:40.060 --> 03:45.550
So let's try this with the multimap we had before. We call find() with "Graham" as the key.

03:46.600 --> 03:50.380
Then this will return an iterator to the first element with key "Graham".

03:52.880 --> 04:00.130
And then we can get the number of elements which have key "Graham", and then we can leap over these elements.

04:00.140 --> 04:02.690
So we just keep incrementing the iterator each time.

04:05.360 --> 04:06.080
And there we are.

04:06.290 --> 04:08.540
We get all the elements with key "Graham".

04:11.300 --> 04:17.240
And if we want to find an element with a particular value, then we can just check that inside the

04:17.240 --> 04:19.040
loop, so like that. So,

04:19.280 --> 04:22.040
is there an element with key "Graham" and value 66?

04:22.490 --> 04:23.090
Yes, there is.

04:24.260 --> 04:25.670
Okay, so that is it for this video.

04:26.090 --> 04:29.060
I will see you next time, but until then, keep coding!
