Us far we've only been using C strings and we've had to be very mindful about how they work.
We always had to account for the null character and we had to use the C string library to copy strings.
I mean I use them for the majority of this course because it's important for you to understand how things
really work under the hood whenever you're programming there should never be anything that you would
consider as magical.
Otherwise you won't understand their implications on the code as a whole.
Understanding your program as a whole is really the key to making it the best it can be.
But instead of using C strings we could have been using C++ strings and to do that we use the string
library case so not C string but string OK and this actually gives us access to a real string type so
we type string like this and give it a name.
We can actually have regular strings as if it was a real type.
OK.
And it is in this case and there's lots of things that we can do with it.
For example we can put it
ministering seek out just like normal K and we can even get it from input so we could actually say please
dear string
this he get.
It's actually making a new string for this string to
actually put it into my string too.
So instead of using what you have been using which is the you tell our our own huge sales library actually
and we wrote a function in there to actually get a string right.
We can just use C in instead and have that kind of nice support.
And even in output it's the same string to this case if you're in this C great solo just like we thought
and we can even get it right.
Everyone even print out every day.
So there's other things we can do which is something really nice which is a pending strings together.
So you could say make another string say string three
and we can actually append them together.
So say my string plus my string to k.
So we're basically adding my string to or reading my string to do my string.
Ok so if we put that say results
is I string three
of your wryness.
Everyone result is Hello everyone so it actually adds them together.
Pens them together they just kind of cool and we could even have done this like this say my string three
sequels Samoa thing that's so sexy.
But for
free again everyone
so everyone actually append this thing this exclamation mark to the string as well.
OK.
And the edition will actually again appends strings together and notice that we don't have to worry
about the memory of the string anymore and we always have to worry about how big the array for this
string was.
But now we don't really have to worry about that because internally the string type we ever hear will
probably do everything for us.
In fact it will do everything for us though it internally it might do some dynamic memory allocations
but we don't really have to worry that the string itself will take care of it.
One other nice thing is that just like our C strings we can actually set characters directly.
So when you actually use the indexing operator they like this.
Just like a C string.
So it's just like with C strings.
So we can actually access individual elements individual characters.
In this case of the string itself the other nice thing about String type is that we can actually get
the length of the string.
Unlike with C string before which we have to use a function to do that this type actually provides the
length already.
So if I said zero length
string is or of say just say my string theory is and we said My string three dot like
if you write and that's everyone
and the length of my string is 14 or so.
One two three four five six seven eight nine 10 11 12 13 14 insets 14 characters so that's the night.
That's nice.
And so we don't have to use Sterling anymore for string lengths.
And one very interesting part of the string type is we can actually create a C string from it.
So I said See
C string tension is say my string.
3. C string.
Stir.
So this actually gives you a conc char star.
OK.
So it's a constant character pointer.
Or a pointer to a character constant if you will.
Which is really nice.
And you may be wondering why would I care about this if I have this string type now.
Well we care about it because Still if you want to interface with C or C libraries which we will be
doing in the next section
C only has C strings right.
That's why that's why they're called Caesarians factors because they're from the C language.
And in order to interface with some of the functions that we're going to need we actually have to use
this dot C string function.
But we'll see that in the next section.
One thing to remember in a very important thing is if we're using strings with functions so if we had
some function like this
if you do if you did this
hey this is passing it by value.
And the reason why I went through all the trouble of showing you see strings before is we know that
in this case it would actually do copy OK.
So it actually copy all the values in the string K which is all the characters in what basically amounts
to is the big array into this function k.
So it will actually do a real copy which could be very slow.
So instead of doing this with strings you should always be doing the pass by reference.
Or if you don't need to change it pass it by constant reference.
Otherwise you'll actually incur potentially a very big penalty.
It will be very slow.
It will waste a lot of memory.
OK.
So just remember that when you're passing strings to functions it should be either by reference or by
constant reference.
Or if you really need to change something like string pointer OK so other than that they really just
work like any other type.
So we'll be using them from now on our next game.
And and actually in the last lecture of this section which will be working on the mini project that
we started in the last section a bit more so in the next lecture I want to go over vector's.