Hello again! In this video, we are going to look at adding elements to strings.
The string class has a member function called append. If we call this on a string.
it arguments is going to be added to that string, at the back.
So if we have a string whose data is "Hello" and we append another string whose data is " world", then the
first string ends up containing the data "Hello world".
And we could also do this using the plus equals operator.
So these come from the two different interfaces to the string class. The plus equals is from the original
implementation, which made it look like a built-in type, and the append member function is to make
it more like a standard container.
The main difference is that you can do a lot more with append.
It takes a lot more argument types, although it does not take a single character.
So if you want that, you need the plus equals operator.
As an example, you can use a substring directly without having to create a substring object.
So we have a string "wow!!!!" with four exclamation marks.
We have an offset three, which is the element which is the first exclamation mark and then the count
is two.
So we're going to append these two exclamation marks, and then the string will be "Hello world double exclamation
mark".
So here is that code. We are taking "Hello", and appending "world" to it. And then we take another
string "Hello" and append the exclamation marks to it.
And we get "Hello world" and "Hello", exclamation exclamation.
Something that you'll probably find more useful is the insert member function.
So again, this is part of the container interface.
So all the containers have a similar function.
In this case, it takes an offset, which is the index where the string is going to be inserted
and the string object that it is going to insert.
Two is the index of the letter 'r' and then this is going to insert "lte" before the letter 'r', so the
result of that is going to be that the string will contain the data "folder".
And as another example, if we have a string "care" and "omp" and then at index one.
So that is before the letter a. It is going to insert "omp".
So we end up with "compare".
And there is that code.
So we get "folder" and "compare".
Some more things you can do.
You can use a substring.
So if we give a string object, offset, count, then it will insert that part of the string, into the string
that it is called on.
So if we have a string with "trombone", that is going to be the letters 'b' and 'o', and then it is going
to insert that before the second letter 'x'.
So we end up with "xbox".
And if any of Microsoft's lawyers are watching, this is a trademark, registered copyright,
all rights reserved.
Please don't sue me!
We can also insert a single char, multiple times.
So offset one, so that is before the 'a' in "cash". We are going to insert the letter 'o' three times, so we
should get "crrrash".
And so here is the code for those. So we have "trombone"
into "xx", we have 'r' into "cash" and then one where we use the return value from a find operation.
So you remember that find will return an index.
So that is the index of the first matching character.
So if we could find with argument 'o', it is going to be the index of that element. And then we can pass that
index as the argument to a call to insert.
So "opos" will be the position of 'o'. And we are being good careful programmers, so we make sure that
we have actually got a valid index before we use it, because something might change and then we would be
accessing an invalid element.
And then we insert the letter 'o' twice, so that will go before the first occurrence of 'o' in this string.
So there we are, we get "xbox", "crrrash" and "Hellooo".
We can also use iterators with insert.
You remember that the end iterator it will return the last plus one character.
And if we subtract one from that, we get an iterator to the last character in the string. And then
we can use that iterator as the first argument to insert.
And this will insert the character 'l' before the last character of the string, which is the letter 'd'.
So the string will now be "world".
And we can also use the end iterator directly.
That is the last character plus one, so if we insert before that, it go before the last character plus
one, but after the last character.
So if we get this end iterator and insert 'l' twice after it, this will insert 'l' twice after
the last character and we get "skill".
So let's try that.
And there it is. So we get "world" and "skill".
Something that you need to be careful about when you're using iterators with insert statements. The insert
operation may cause any iterators that you had before the operation to become invalid.
For example, the string has an internal memory buffer where it stores all the data.
If that memory buffer is just big enough to store all the data it has, and you add some more data, then
the buffer will no longer be big enough.
In that case, the string is going to allocate a new buffer and copy all the data into the new buffer
and then delete the old one.
So if you had an iterator to the element when it was in the old buffer, then that iterator will no
longer be valid, because that buffer is no longer part of the string.
So that element is not part of the string's data.
So as an example, if you get an iterator to the first element of a string and then you add some more
data to the string.
So in this case, we are adding the letter 'd' 16 times at the back of the string, and that is enough to
cause the string to be reallocate its memory buffer. Or at least, it has been on every compiler that I have
tried it with.
And then all the data now goes into this new buffer.
You would think that just inserting some elements would not change the position of the first element.
But in fact, that is now pointing to an element that is not in the buffer.
And you will get a memory error when the program runs.
So here is the code. We have our string.
We get an iterator to the first element. And then we insert the letter 'd' 16 times at the back of the string.
and then we try to insert a character at the front of the string.
So let's try that.
Right!
Okay, so the program has crashed.
So the thing to do here is to, if you like, "refresh" the iterator. So we need to get a new iterator
to the first element in the string.
(So I can paste that back in)
So we reassign the first voter, so it is now at the new first element.
And then it should all work.
Yep, there we are.
OK, so that is one thing you need to watch out for with iterators and insert.
Okay, so that's it for this video.
I'll see you next time.
But meanwhile, keep coding!