string
Figure 10.1 demonstrates many of class string
’s overloaded operators and several other useful member functions, including empty
, substr
and at
. Function empty
determines whether a string
is empty, function substr
(for “substring”) returns a string
that’s a portion of an existing string
and function at
returns the character at a specific index in a string
(after checking that the index is in range). Chapter 21 presents class string
in detail.
string
Objects and Displaying Them with cout
and Operator <<Lines 8–10 create three string
objects:
s1
is initialized with the literal "happy"
,
s2
is initialized with the literal " birthday"
and
s3
uses the default string
constructor to create an empty string
.
Lines 13–14 output these three objects, using cout
and operator <<
, which the string
class designers overloaded to handle string
objects.
string
Objects with the Equality and Relational OperatorsLines 15–21 show the results of comparing s2
to s1
by using class string
’s overloaded equality and relational operators, which perform lexicographical comparisons (that is, like a dictionary ordering) using the numerical values of the characters in each string
(see Appendix B, ASCII Character Set).
Normally, when you output a condition’s value 0
is displayed for false or 1
for true. However, we used the stream manipulator boolalpha
(line 15) to set the output stream to display condition values as the strings "false"
and "true"
.
string
Member Function empty
Line 26 uses string
member function empty
, which returns true
if the string
is empty; otherwise, it returns false
. The object s3
was initialized with the default constructor, so it is indeed empty.
string
Copy Assignment OperatorLine 28 demonstrates class string
’s overloaded copy assignment operator by assigning s1
to s3
. Line 29 outputs s3
to demonstrate that the assignment worked correctly.
string
Concatenation and C++14 string
-object LiteralsLine 34 demonstrates class string
’s overloaded +=
operator for string concatenation assignment. In this case, the contents of s2
are appended to s1
, thus modifying its value. Then line 35 outputs the resulting string
that’s stored in s1
. Line 39 demonstrates that you also may append a C-string literal to a string
object by using operator +=
. Line 40 displays the result.
Similarly, line 44 concatenates s1
with a C++14 string-object literal
which is indicated by placing the letter s
immediately following the closing "
of a string
literal, as in
", have a great day!"s
The preceding literal actually results in a call to a C++ Standard Library function that returns a string
object containing the literal’s characters. Line 45 displays the new value of s1
.
string
Member Function substr
Class string
provides member function substr
(lines 50 and 54) to return a string
containing a portion of the string
object on which the function is called. The call in line 50 obtains a 14-character substring (specified by the second argument) of s1
starting at position 0 (specified by the first argument). The call to substr
in line 54 obtains a substring starting from position 15 of s1
. When the second argument is not specified, substr
returns the remainder of the string
on which it’s called.
string
Copy ConstructorLine 57 creates string
object s4
and initializes it with a copy of s1
. This calls class string
’s copy constructor, which copies the contents of s1
into the new object s4
. You’ll see how to define a custom copy constructor for your own class in Section 10.10.
string
Copy Assignment OperatorLine 62 uses class string
’s overloaded copy assignment (=
) operator to demonstrate that it handles self-assignment properly—we’ll see when we build class Array
later in the chapter that self-assignment can be dangerous for objects that manage their own memory, and we’ll show how to deal with the issues. Line 63 confirms that s4
still has the same value after the self-assignment.
string
Class [] OperatorLines 66–67 use class string
’s overloaded []
operator to create lvalues that enable new characters to replace existing characters in s1
. Lines 68–69 output the new value of s1
. Class string
’s overloaded []
operator does not perform any bounds checking. Therefore, you must ensure that operations using class string
’s overloaded []
operator do not accidentally manipulate elements outside the bounds of the string
. Class string
does provide bounds checking in its member function at
, which throws an exception if its argument is an invalid subscript. If the subscript is valid, function at
returns the character at the specified location as a modifiable lvalue or a nonmodifiable lvalue (e.g., a const
reference), depending on the context in which the call appears. For example:
If at
is called on a non-const
string
object, the function returns a modifiable lvalue, which could be used on the left of an assignment (=
) operator to assign a new value to that location in the string
.
If at
is called on a const
string
object, the function returns a nonmodifiable lvalue that can be used to obtain, but not modify, the value at that location in the string
.
Line 74 demonstrates a call to function at
with an invalid subscript; this throws an out_of_range
exception.