Lecture 3 - Slide 11 : 27
C++ style text strings
It is recommended to use type
std::string
instead of the low-level
char*
or
char[]
The C++ Prog. Lang. (3. edition)
:
Page 48-49, 579
The C++ Prog. Lang. (4. edition)
: Page 90-91, 1033
Type
string
in C++:
string
is alias for a
basic_string
parameterized by
char:
typedef basic_string<char> string
Strings has
value semantics
Copied in and out of functions - or passed by reference
Characters in strings can be accessed in
checked mode
and
unchecked mode
:
Checked mode:
s.at(i)
Unchecked mode:
s[i]
A string is
mutable
Strings can be
copy constructed
based on a C-style string
string s{"Peter"};
or
string s("Peter");
or
string t = "Peter";
The
basic_string
class supports a large number of string operations
Short string optimization:
Short strings are allocated in the string object itself (not on the free store)
Strings in C#