WEBVTT

00:00.910 --> 00:06.710
Us far we've only been using C strings and we've had to be very mindful about how they work.

00:06.720 --> 00:13.200
We always had to account for the null character and we had to use the C string library to copy strings.

00:13.290 --> 00:18.420
I mean I use them for the majority of this course because it's important for you to understand how things

00:18.420 --> 00:23.520
really work under the hood whenever you're programming there should never be anything that you would

00:23.520 --> 00:25.980
consider as magical.

00:26.130 --> 00:31.140
Otherwise you won't understand their implications on the code as a whole.

00:31.170 --> 00:36.070
Understanding your program as a whole is really the key to making it the best it can be.

00:38.470 --> 00:48.010
But instead of using C strings we could have been using C++ strings and to do that we use the string

00:48.010 --> 01:01.520
library case so not C string but string OK and this actually gives us access to a real string type so

01:01.790 --> 01:06.800
we type string like this and give it a name.

01:08.380 --> 01:14.440
We can actually have regular strings as if it was a real type.

01:14.440 --> 01:14.910
OK.

01:15.030 --> 01:21.840
And it is in this case and there's lots of things that we can do with it.

01:21.840 --> 01:23.640
For example we can put it

01:26.810 --> 01:40.220
ministering seek out just like normal K and we can even get it from input so we could actually say please

01:40.710 --> 01:42.020
dear string

01:45.110 --> 01:50.160
this he get.

01:50.660 --> 01:55.210
It's actually making a new string for this string to

01:58.440 --> 02:01.220
actually put it into my string too.

02:01.230 --> 02:07.610
So instead of using what you have been using which is the you tell our our own huge sales library actually

02:08.230 --> 02:12.340
and we wrote a function in there to actually get a string right.

02:12.570 --> 02:18.750
We can just use C in instead and have that kind of nice support.

02:19.410 --> 02:32.780
And even in output it's the same string to this case if you're in this C great solo just like we thought

02:33.320 --> 02:35.890
and we can even get it right.

02:36.290 --> 02:41.850
Everyone even print out every day.

02:45.770 --> 02:53.780
So there's other things we can do which is something really nice which is a pending strings together.

02:54.200 --> 03:01.590
So you could say make another string say string three

03:04.710 --> 03:06.430
and we can actually append them together.

03:06.440 --> 03:15.860
So say my string plus my string to k.

03:15.950 --> 03:24.410
So we're basically adding my string to or reading my string to do my string.

03:24.620 --> 03:32.020
Ok so if we put that say results

03:34.230 --> 03:37.780
is I string three

03:41.000 --> 03:44.380
of your wryness.

03:44.740 --> 03:50.700
Everyone result is Hello everyone so it actually adds them together.

03:50.740 --> 04:02.040
Pens them together they just kind of cool and we could even have done this like this say my string three

04:03.160 --> 04:10.440
sequels Samoa thing that's so sexy.

04:10.460 --> 04:11.470
But for

04:14.230 --> 04:17.640
free again everyone

04:20.820 --> 04:28.630
so everyone actually append this thing this exclamation mark to the string as well.

04:29.990 --> 04:30.650
OK.

04:31.060 --> 04:37.390
And the edition will actually again appends strings together and notice that we don't have to worry

04:37.390 --> 04:44.200
about the memory of the string anymore and we always have to worry about how big the array for this

04:44.320 --> 04:45.180
string was.

04:45.190 --> 04:54.190
But now we don't really have to worry about that because internally the string type we ever hear will

04:54.250 --> 04:56.680
probably do everything for us.

04:56.680 --> 05:03.440
In fact it will do everything for us though it internally it might do some dynamic memory allocations

05:03.460 --> 05:07.920
but we don't really have to worry that the string itself will take care of it.

05:09.400 --> 05:21.940
One other nice thing is that just like our C strings we can actually set characters directly.

05:22.000 --> 05:29.090
So when you actually use the indexing operator they like this.

05:29.090 --> 05:30.760
Just like a C string.

05:30.940 --> 05:35.620
So it's just like with C strings.

05:37.250 --> 05:41.750
So we can actually access individual elements individual characters.

05:41.750 --> 05:51.740
In this case of the string itself the other nice thing about String type is that we can actually get

05:51.770 --> 05:54.430
the length of the string.

05:55.080 --> 06:02.600
Unlike with C string before which we have to use a function to do that this type actually provides the

06:02.600 --> 06:04.580
length already.

06:04.580 --> 06:08.240
So if I said zero length

06:12.130 --> 06:29.950
string is or of say just say my string theory is and we said My string three dot like

06:34.400 --> 06:38.100
if you write and that's everyone

06:40.350 --> 06:43.110
and the length of my string is 14 or so.

06:43.140 --> 06:54.360
One two three four five six seven eight nine 10 11 12 13 14 insets 14 characters so that's the night.

06:54.420 --> 06:55.330
That's nice.

06:55.330 --> 07:01.220
And so we don't have to use Sterling anymore for string lengths.

07:04.130 --> 07:16.430
And one very interesting part of the string type is we can actually create a C string from it.

07:16.880 --> 07:20.800
So I said See

07:23.510 --> 07:31.500
C string tension is say my string.

07:31.520 --> 07:35.080
3. C string.

07:35.240 --> 07:37.690
Stir.

07:37.860 --> 07:44.280
So this actually gives you a conc char star.

07:44.390 --> 07:44.690
OK.

07:44.700 --> 07:48.390
So it's a constant character pointer.

07:49.330 --> 07:54.690
Or a pointer to a character constant if you will.

07:55.140 --> 07:57.610
Which is really nice.

07:58.050 --> 08:04.840
And you may be wondering why would I care about this if I have this string type now.

08:05.280 --> 08:12.420
Well we care about it because Still if you want to interface with C or C libraries which we will be

08:12.420 --> 08:14.730
doing in the next section

08:17.610 --> 08:21.210
C only has C strings right.

08:21.270 --> 08:26.670
That's why that's why they're called Caesarians factors because they're from the C language.

08:26.670 --> 08:34.640
And in order to interface with some of the functions that we're going to need we actually have to use

08:34.640 --> 08:38.930
this dot C string function.

08:39.530 --> 08:43.620
But we'll see that in the next section.

08:43.650 --> 08:58.270
One thing to remember in a very important thing is if we're using strings with functions so if we had

08:58.270 --> 09:00.550
some function like this

09:03.240 --> 09:05.280
if you do if you did this

09:10.400 --> 09:13.420
hey this is passing it by value.

09:17.170 --> 09:25.660
And the reason why I went through all the trouble of showing you see strings before is we know that

09:27.370 --> 09:33.210
in this case it would actually do copy OK.

09:33.370 --> 09:45.970
So it actually copy all the values in the string K which is all the characters in what basically amounts

09:45.970 --> 09:50.860
to is the big array into this function k.

09:50.890 --> 09:55.500
So it will actually do a real copy which could be very slow.

09:55.840 --> 10:03.740
So instead of doing this with strings you should always be doing the pass by reference.

10:04.210 --> 10:09.240
Or if you don't need to change it pass it by constant reference.

10:09.250 --> 10:15.030
Otherwise you'll actually incur potentially a very big penalty.

10:15.030 --> 10:16.860
It will be very slow.

10:16.860 --> 10:18.580
It will waste a lot of memory.

10:18.810 --> 10:19.620
OK.

10:19.620 --> 10:26.370
So just remember that when you're passing strings to functions it should be either by reference or by

10:26.910 --> 10:28.470
constant reference.

10:28.470 --> 10:37.880
Or if you really need to change something like string pointer OK so other than that they really just

10:37.880 --> 10:41.260
work like any other type.

10:41.600 --> 10:49.820
So we'll be using them from now on our next game.

10:49.820 --> 10:56.420
And and actually in the last lecture of this section which will be working on the mini project that

10:56.420 --> 11:04.280
we started in the last section a bit more so in the next lecture I want to go over vector's.
