Hello again! In this video, we are going to start looking at the container classes in the C++ standard
library.
We have already met the string and vector.
These are examples of sequential containers. In these containers,
the order of the data can be whatever the program wants it to be.
It does not depend on the value of the data.
When we put some data in a sequential container, it will stay in the same position, unless something happens
to change the order of the elements.
If we add or remove elements or sort the container, for example. Sequential containers are useful
if we want to be able to choose the order in which we store the data.
In most cases, but not all, we can use the position of the data to access an element.
For example, with string and vector, we can use an index.
In an associative container, the order depends on the data.
Each element has some key which will identify it.
This key is used to look up in an element in the container, and the position of the element in the
container depends on the value of the key.
These containers are useful if we want to store information in a way which is easy to search.
There are two types of associative container in C++.
We have the set, which is just a collection of elements, without any structure.
The elements consist of just a key, and nothing else.
We can use these to check if a particular key is present or not present in the collection.
The map is similar to the directory or hash map, which other languages provide.
The elements consist of two components.
There is a key and a value.
The value contains the actual data for the element, and the key is something we can use to search
for the element and look up its value.
In an associative container, the position of an element depends entirely on its key.
There is no notion of a front or a back to the container, so they do not support operations like
push_front() or push_back().
Many of the usual container operations are supported.
We can use insert() and erase() to add or remove elements, and we can use iterators, so we can loop over
some or all of the elements in the container.
And there are member functions which are similar to some of the generic algorithms.
For example, find(), and sort() and so on.
Finally, there are also some so-called "container adaptors".
These are implemented on top of sequential containers.
So these are queues and stacks.
And in these containers, the order of the data depends on when the data was added to the container.
OK, so that is it for this video.
I will see you next time, but until then, keep coding!