Hello again! In this video, we are going to look at maps and insertion.
The reason for this is that there are some new features in C++ 17 for working with maps, and we need
a bit more background before we can go onto those.
So just to recap, we know two ways of inserting an element into a map.
These are the subscript operator and the insert() member function. Looking first at the subscript operator.
this provides so-called "insert or assign" functionality.
So if there is no element in the map which has the key we are using, this will insert a new element. If
there already is an element with that key,
it will assign to the value of that element.
So if we have some code like this: we create a map object, and then later on, we have a statement
like this.
There are two possible outcomes depending on what happens in here. Either there is no "Graham" element,
in which case we can insert one, or there already is a "Graham" element, in which case we overwrite it.
So sometimes we might want to know whether we have actually created a new element or not.
And unfortunately, there is no way to know.
Looking at the actual way that the subscript operator inserts an element, when it does insert one.
This is done in two stages.
So first, it will create the new element.
The key is going to be the argument to the subscript, and the value is going to be default-initialized.
So this means that the value type has to be either a built in type or something that has a default constructor.
And that can be an irritating restriction sometimes.
Also, if an exception gets thrown, probably by the assignment.
We have not actually covered exceptions yet,
I know. But, basically, what happens is that this operation will end before it has finished doing all its
work.
And in that case, we end up with a partially populated element.
So it has a key, but
the value is default-initialized.
Also, if we forget to do the assignment part, then we end up with a partially populated element as
well.
So this will have the key "Graham" and an undefined value, or possibly default-initialized.
And the return value from the operation is a reference to the value member of the element, so that tells
us nothing about what happened.
The insert member will only insert a new element - at least it will only directly insert one. If you want
to do anything else, you have to write a bit more code.
If the map already has an element with the same key,
nothing happens.
And the return value from insert() will actually tell us whether we actually inserted an element or not.
There is no requirement that the type of the value has a default constructor.
And if an exception is thrown, then the insertion has no effect. So the operation does get rolled
back.
So the insert() member function has some advantages over subscripts, but it does not perform "insert
or assign".
If we want that, we have to write code like this.
So we do the insert. Then we get the return value. Then we check the second member.
And if that is false, then we need to do the assignment.
So it is nothing very complicated, but it is five statements for something which really is a single logical
operation.
So let's try this out with the map that we had before.
We call insert(). And then, if the insert() does not succeed, we need to assign to the value member of this
element.
So there it is, we already had an element with the key "Graham".
So we needed to modify the value.
OK, so that is it for this video.
I will see you next time, but until then, keep coding!