WEBVTT

00:01.210 --> 00:05.320
Hello again! In this video, we are going to look at adding elements to strings.

00:07.910 --> 00:13.130
The string class has a member function called append. If we call this on a string.

00:13.440 --> 00:16.520
it arguments is going to be added to that string, at the back.

00:16.790 --> 00:22.910
So if we have a string whose data is "Hello" and we append another string whose data is " world", then the

00:22.910 --> 00:26.210
first string ends up containing the data "Hello world".

00:28.410 --> 00:31.530
And we could also do this using the plus equals operator.

00:33.480 --> 00:39.420
So these come from the two different interfaces to the string class. The plus equals is from the original

00:39.420 --> 00:44.370
implementation, which made it look like a built-in type, and the append member function is to make

00:44.370 --> 00:46.200
it more like a standard container.

00:47.850 --> 00:50.310
The main difference is that you can do a lot more with append.

00:50.370 --> 00:54.480
It takes a lot more argument types, although it does not take a single character.

00:54.810 --> 00:57.390
So if you want that, you need the plus equals operator.

00:58.830 --> 01:04.740
As an example, you can use a substring directly without having to create a substring object.

01:05.760 --> 01:09.480
So we have a string "wow!!!!" with four exclamation marks.

01:10.320 --> 01:17.460
We have an offset three, which is the element which is the first exclamation mark and then the count

01:17.460 --> 01:17.860
is two.

01:17.880 --> 01:25.320
So we're going to append these two exclamation marks, and then the string will be "Hello world double exclamation

01:25.320 --> 01:25.620
mark".

01:28.330 --> 01:33.730
So here is that code. We are taking "Hello", and appending "world" to it. And then we take another

01:33.730 --> 01:37.330
string "Hello" and append the exclamation marks to it.

01:40.060 --> 01:43.780
And we get "Hello world" and "Hello", exclamation exclamation.

01:46.340 --> 01:50.060
Something that you'll probably find more useful is the insert member function.

01:50.540 --> 01:52.640
So again, this is part of the container interface.

01:52.700 --> 01:55.610
So all the containers have a similar function.

01:58.290 --> 02:04.590
In this case, it takes an offset, which is the index where the string is going to be inserted

02:04.590 --> 02:07.350
and the string object that it is going to insert.

02:09.330 --> 02:17.280
Two is the index of the letter 'r' and then this is going to insert "lte" before the letter 'r', so the

02:17.280 --> 02:22.950
result of that is going to be that the string will contain the data "folder".

02:24.420 --> 02:30.630
And as another example, if we have a string "care" and "omp" and then at index one.

02:31.080 --> 02:34.260
So that is before the letter a. It is going to insert "omp".

02:35.460 --> 02:37.050
So we end up with "compare".

02:39.200 --> 02:40.910
And there is that code.

02:43.490 --> 02:46.010
So we get "folder" and "compare".

02:48.450 --> 02:50.040
Some more things you can do.

02:51.030 --> 02:52.500
You can use a substring.

02:52.740 --> 03:00.960
So if we give a string object, offset, count, then it will insert that part of the string, into the string

03:00.960 --> 03:01.650
that it is called on.

03:02.070 --> 03:07.920
So if we have a string with "trombone", that is going to be the letters 'b' and 'o', and then it is going

03:07.920 --> 03:10.860
to insert that before the second letter 'x'.

03:11.490 --> 03:13.170
So we end up with "xbox".

03:13.590 --> 03:18.170
And if any of Microsoft's lawyers are watching, this is a trademark, registered copyright,

03:18.180 --> 03:19.050
all rights reserved.

03:19.380 --> 03:20.190
Please don't sue me!

03:22.120 --> 03:24.970
We can also insert a single char, multiple times.

03:28.030 --> 03:34.630
So offset one, so that is before the 'a' in "cash". We are going to insert the letter 'o' three times, so we

03:34.630 --> 03:35.940
should get "crrrash".

03:39.750 --> 03:42.630
And so here is the code for those. So we have "trombone"

03:43.750 --> 03:50.410
into "xx", we have 'r' into "cash" and then one where we use the return value from a find operation.

03:50.710 --> 03:54.430
So you remember that find will return an index.

03:54.760 --> 03:56.950
So that is the index of the first matching character.

03:56.980 --> 04:03.430
So if we could find with argument 'o', it is going to be the index of that element. And then we can pass that

04:03.430 --> 04:07.570
index as the argument to a call to insert.

04:08.140 --> 04:14.890
So "opos" will be the position of 'o'. And we are being good careful programmers, so we make sure that

04:14.890 --> 04:21.490
we have actually got a valid index before we use it, because something might change and then we would be

04:21.490 --> 04:23.140
accessing an invalid element.

04:25.340 --> 04:32.210
And then we insert the letter 'o' twice, so that will go before the first occurrence of 'o' in this string.

04:37.190 --> 04:40.900
So there we are, we get "xbox", "crrrash" and "Hellooo".

04:48.130 --> 04:51.070
We can also use iterators with insert.

04:52.890 --> 04:56.610
You remember that the end iterator it will return the last plus one character.

04:56.970 --> 05:02.280
And if we subtract one from that, we get an iterator to the last character in the string. And then

05:02.280 --> 05:05.700
we can use that iterator as the first argument to insert.

05:06.300 --> 05:11.970
And this will insert the character 'l' before the last character of the string, which is the letter 'd'.

05:12.690 --> 05:14.610
So the string will now be "world".

05:16.240 --> 05:18.820
And we can also use the end iterator directly.

05:19.180 --> 05:24.700
That is the last character plus one, so if we insert before that, it go before the last character plus

05:24.700 --> 05:27.100
one, but after the last character.

05:28.600 --> 05:36.010
So if we get this end iterator and insert 'l' twice after it, this will insert 'l' twice after

05:36.010 --> 05:38.380
the last character and we get "skill".

05:43.470 --> 05:45.120
So let's try that.

05:47.540 --> 05:51.620
And there it is. So we get "world" and "skill".

05:57.110 --> 06:03.200
Something that you need to be careful about when you're using iterators with insert statements. The insert

06:03.200 --> 06:08.840
operation may cause any iterators that you had before the operation to become invalid.

06:09.410 --> 06:13.670
For example, the string has an internal memory buffer where it stores all the data.

06:14.060 --> 06:20.150
If that memory buffer is just big enough to store all the data it has, and you add some more data, then

06:20.150 --> 06:22.130
the buffer will no longer be big enough.

06:22.610 --> 06:28.430
In that case, the string is going to allocate a new buffer and copy all the data into the new buffer

06:28.430 --> 06:29.570
and then delete the old one.

06:30.380 --> 06:36.440
So if you had an iterator to the element when it was in the old buffer, then that iterator will no

06:36.440 --> 06:39.290
longer be valid, because that buffer is no longer part of the string.

06:39.770 --> 06:42.230
So that element is not part of the string's data.

06:46.140 --> 06:51.390
So as an example, if you get an iterator to the first element of a string and then you add some more

06:51.390 --> 06:52.470
data to the string.

06:52.860 --> 06:59.490
So in this case, we are adding the letter 'd' 16 times at the back of the string, and that is enough to

06:59.490 --> 07:05.340
cause the string to be reallocate its memory buffer. Or at least, it has been on every compiler that I have

07:05.340 --> 07:05.880
tried it with.

07:07.400 --> 07:10.340
And then all the data now goes into this new buffer.

07:11.240 --> 07:16.580
You would think that just inserting some elements would not change the position of the first element.

07:17.090 --> 07:20.270
But in fact, that is now pointing to an element that is not in the buffer.

07:20.780 --> 07:23.030
And you will get a memory error when the program runs.

07:26.250 --> 07:27.990
So here is the code. We have our string.

07:28.560 --> 07:35.580
We get an iterator to the first element. And then we insert the letter 'd' 16 times at the back of the string.

07:36.720 --> 07:40.770
and then we try to insert a character at the front of the string.

07:42.090 --> 07:43.170
So let's try that.

07:46.100 --> 07:46.460
Right!

07:47.450 --> 07:49.040
Okay, so the program has crashed.

07:55.930 --> 08:02.010
So the thing to do here is to, if you like, "refresh" the iterator. So we need to get a new iterator

08:02.040 --> 08:03.730
to the first element in the string.

08:07.220 --> 08:08.660
(So I can paste that back in)

08:09.530 --> 08:14.900
So we reassign the first voter, so it is now at the new first element.

08:15.200 --> 08:16.370
And then it should all work.

08:18.900 --> 08:19.680
Yep, there we are.

08:22.470 --> 08:25.860
OK, so that is one thing you need to watch out for with iterators and insert.

08:26.430 --> 08:28.020
Okay, so that's it for this video.

08:28.410 --> 08:29.280
I'll see you next time.

08:29.290 --> 08:31.110
But meanwhile, keep coding!
