- The STL String class is a special type
of containers specifically designed to operate
with sequences of characters.
It's designed with many features to operate
on strings efficiently and intuitively.
This is string.cpp from chapter nine of the exercise files.
String is an STL container class that operates on characters
in the same way as any sequence container class operates
on other types.
String has functions that are specifically designed
for operating on strings of characters.
It works a lot like vector but with additional string
related functions.
We're including the string header here,
which is where the string class and its associated libraries
are located.
And it's in the standard namespace like the rest of the STL.
So here we declare a string and a string iterator.
Again, the string iterator is declared with the string
class, followed by the scope operator and the word iterator.
And this is true of most of the iterators in the STL,
we're just going to go ahead and build and run this so
we can follow along.
We've initialized our string with a C string that says
this is a string.
And you notice that we're getting the size and the length,
these are the same function, they're identical.
And the only reason for length here is that sometimes
you expect that word for the size of a string.
And you'll notice down here it says,
the length and the size are both 16.
And that's what we expect to believe this is 16 characters,
that's five, and that's 10 and that's 16, Yeah.
The addition operator is used for concatenation.
And so we can concatenate strings with the plus operator,
and we can add in a colon and another string.
And when we print it out,
we notice that we get this is a string colon,
this is also a string concatenated together,
we can compare strings with the standard comparison
operators equal to greater than, less than,
and you notice that we get the results that we expect,
based on the correlation order of the language
which that you're using.
We can iterate through a string,
and in this case, we're using the range based for loop.
And this works just like it does with a vector
or with any other container.
And here we're printing out each of the characters
of the string with a space between them.
We can use insert and erase just as we can
with any container class.
And we can even use replace,
where we take the two characters starting at position five,
which says is and replaced with ain't
and now we say instead of this is a string,
it says this ain't a string.
We can get substrings and here we get those five characters.
We can find characters, either from the beginning
or from the end with finding our find.
And that's just the most common of the string methods
available in the string class in the STL.
The STL String class is a powerful and flexible string class
implemented as a sequence container.
It has a rich set of features and an intuitive interface,
for more detail on STL strings,
please see the companion course C++ Templates in the STL.