WEBVTT

00:00.150 --> 00:05.610
Hello again! In this video, we are going to start looking at the container classes in the C++ standard

00:05.700 --> 00:06.150
library.

00:07.290 --> 00:09.540
We have already met the string and vector.

00:09.840 --> 00:13.730
These are examples of sequential containers. In these containers,

00:13.740 --> 00:17.220
the order of the data can be whatever the program wants it to be.

00:17.490 --> 00:19.530
It does not depend on the value of the data.

00:20.490 --> 00:25.890
When we put some data in a sequential container, it will stay in the same position, unless something happens

00:25.890 --> 00:27.870
to change the order of the elements.

00:28.350 --> 00:34.530
If we add or remove elements or sort the container, for example. Sequential containers are useful

00:34.530 --> 00:37.470
if we want to be able to choose the order in which we store the data.

00:38.430 --> 00:43.650
In most cases, but not all, we can use the position of the data to access an element.

00:44.160 --> 00:46.860
For example, with string and vector, we can use an index.

00:49.210 --> 00:53.050
In an associative container, the order depends on the data.

00:53.920 --> 00:57.130
Each element has some key which will identify it.

00:58.090 --> 01:03.430
This key is used to look up in an element in the container, and the position of the element in the

01:03.430 --> 01:05.680
container depends on the value of the key.

01:06.790 --> 01:12.010
These containers are useful if we want to store information in a way which is easy to search.

01:13.240 --> 01:16.180
There are two types of associative container in C++.

01:16.750 --> 01:20.140
We have the set, which is just a collection of elements, without any structure.

01:20.890 --> 01:23.470
The elements consist of just a key, and nothing else.

01:24.130 --> 01:28.780
We can use these to check if a particular key is present or not present in the collection.

01:31.050 --> 01:35.370
The map is similar to the directory or hash map, which other languages provide.

01:36.300 --> 01:38.610
The elements consist of two components.

01:39.060 --> 01:40.980
There is a key and a value.

01:41.520 --> 01:46.110
The value contains the actual data for the element, and the key is something we can use to search

01:46.110 --> 01:47.720
for the element and look up its value.

01:49.750 --> 01:55.030
In an associative container, the position of an element depends entirely on its key.

01:55.570 --> 02:01.900
There is no notion of a front or a back to the container, so they do not support operations like

02:01.900 --> 02:03.010
push_front() or push_back().

02:04.870 --> 02:07.690
Many of the usual container operations are supported.

02:08.110 --> 02:14.710
We can use insert() and erase() to add or remove elements, and we can use iterators, so we can loop over

02:14.980 --> 02:17.260
some or all of the elements in the container.

02:17.860 --> 02:21.790
And there are member functions which are similar to some of the generic algorithms.

02:22.480 --> 02:25.150
For example, find(), and sort() and so on.

02:28.440 --> 02:31.290
Finally, there are also some so-called "container adaptors".

02:31.890 --> 02:34.980
These are implemented on top of sequential containers.

02:35.520 --> 02:37.440
So these are queues and stacks.

02:38.250 --> 02:43.530
And in these containers, the order of the data depends on when the data was added to the container.

02:44.610 --> 02:46.110
OK, so that is it for this video.

02:46.560 --> 02:49.380
I will see you next time, but until then, keep coding!
