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
returns a string
that represents 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 19 presents class string
in detail.
1 // Fig. 10.1: fig10_01.cpp
2 // Standard Library string class test program.
3 #include <iostream>
4 #include <string>
5 using namespace std;
6
7 int main()
8 {
9 string s1( "happy" );
10 string s2( " birthday" );
11 string s3;
12
13 // test overloaded equality and relational operators
14 cout << "s1 is "" << s1 << ""; s2 is "" << s2
15 << ""; s3 is "" << s3 << '"'
16 << "
The results of comparing s2 and s1:"
17 << "
s2 == s1 yields " << ( s2 == s1 ? "true" : "false" )
18 << "
s2 != s1 yields " << ( s2 != s1 ? "true" : "false" )
19 << "
s2 > s1 yields " << ( s2 > s1 ? "true" : "false" )
20 << "
s2 < s1 yields " << ( s2 < s1 ? "true" : "false" )
21 << "
s2 >= s1 yields " << ( s2 >= s1 ? "true" : "false" )
22 << "
s2 <= s1 yields " << ( s2 <= s1 ? "true" : "false" );
23
24 // test string member-function empty
25 cout << "
Testing s3.empty():" << endl;
26
27 if ( s3.empty() )
28 {
29 cout << "s3 is empty; assigning s1 to s3;" << endl;
30 s3 = s1; // assign s1 to s3
31 cout << "s3 is "" << s3 << """;
32 } // end if
33
34 // test overloaded string concatenation operator
35 cout << "
s1 += s2 yields s1 = ";
36 s1 += s2; // test overloaded concatenation
37 cout << s1;
38
39 // test overloaded string concatenation operator with a C string
40 cout << "
s1 += " to you" yields" << endl;
41 s1 += " to you";
42 cout << "s1 = " << s1 << "
";
43
44 // test string member function substr
45 cout << "The substring of s1 starting at location 0 for
"
46 << "14 characters, s1.substr(0, 14), is:
"
47 << s1.substr( 0, 14 ) << "
";
48
49 // test substr "to-end-of-string" option
50 cout << "The substring of s1 starting at
"
51 << "location 15, s1.substr(15), is:
"
52 << s1.substr( 15 ) << endl;
53
54 // test copy constructor
55 string s4( s1 );
56 cout << "
s4 = " << s4 << "
";
57
58 // test overloaded copy assignment (=) operator with self-assignment
59 cout << "assigning s4 to s4" << endl;
60 s4 = s4;
61 cout << "s4 = " << s4 << endl;
62
63 // test using overloaded subscript operator to create lvalue
64 s1[ 0 ] = 'H';
65 s1[ 6 ] = 'B';
66 cout << "
s1 after s1[0] = 'H' and s1[6] = 'B' is: "
67 << s1 << "
";
68
69 // test subscript out of range with string member function "at"
70 try
71 {
72 cout << "Attempt to assign 'd' to s1.at( 30 ) yields:" << endl;
73 s1.at( 30 ) = 'd'; // ERROR: subscript out of range
74 } // end try
75 catch ( out_of_range &ex )
76 {
77 cout << "An exception occurred: " << ex.what() << endl;
78 } // end catch
79 } // end main
s1 is "happy"; s2 is " birthday"; s3 is ""
The results of comparing s2 and s1:
s2 == s1 yields false
s2 != s1 yields true
s2 > s1 yields false
s2 < s1 yields true
s2 >= s1 yields false
s2 <= s1 yields true
Testing s3.empty():
s3 is empty; assigning s1 to s3;
s3 is "happy"
s1 += s2 yields s1 = happy birthday
s1 += " to you" yields
s1 = happy birthday to you
The substring of s1 starting at location 0 for
14 characters, s1.substr(0, 14), is:
happy birthday
The substring of s1 starting at
location 15, s1.substr(15), is:
to you
s4 = happy birthday to you
assigning s4 to s4
s4 = happy birthday to you
s1 after s1[0] = 'H' and s1[6] = 'B' is: Happy Birthday to you
Attempt to assign 'd' to s1.at( 30 ) yields:
An exception occurred: invalid string position
Lines 9–11 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 14–15 output these three objects, using cout
and operator <<
, which the string
class designers overloaded to handle string
objects. Then lines 16–22 show the results of comparing s2
to s1
by using class string
’s overloaded equality and relational operators, which perform lexicographical comparisons (i.e., like a dictionary ordering) using the numerical values of the characters (see Appendix B, ASCII Character Set) in each string
.
Class string
provides member function empty to determine whether a string
is empty, which we demonstrate in line 27. Member function empty
returns true
if the string
is empty; otherwise, it returns false
.
Line 30 demonstrates class string
’s overloaded copy assignment operator by assigning s1
to s3
. Line 31 outputs s3
to demonstrate that the assignment worked correctly.
Line 36 demonstrates class string
’s overloaded +=
operator for string concatenation. In this case, the contents of s2
are appended to s1
. Then line 37 outputs the resulting string that’s stored in s1
. Line 41 demonstrates that a string literal can be appended to a string
object by using operator +=
. Line 42 displays the result.
Class string
provides member function substr (lines 47 and 52) to return a portion of a string as a string
object. The call to substr
in line 47 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 52 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.
Line 55 creates string
object s4
and initializes it with a copy of s1
. This results in a call to class string
’s copy constructor. Line 60 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 and we’ll show how to deal with the issues.
Lines 64–65 use class string
’s overloaded []
operator to create lvalues that enable new characters to replace existing characters in s1
. Line 67 outputs the new value of s1
. Class string’s overloaded [] operator does not perform any bounds checking. Therefore, you must ensure that operations using standard 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 an nonmodifiable lvalue (e.g., a const
reference), depending on the context in which the call appears. Line 73 demonstrates a call to function at
with an invalid subscript; this throws an out_of_range
exception.
18.220.90.54