Figure 19.1 demonstrates string assignment and concatenation. Line 4 includes header <string>
for class string
. The string
s string1
, string2
and string3
are created in lines 9–11. Line 13 assigns the value of string1
to string2
. After the assignment takes place, string2
is a copy of string1
. Line 14 uses member function assign to copy string1
into string3
. A separate copy is made (i.e., string1
and string3
are independent objects). Class string
also provides an overloaded version of member function assign
that copies a specified number of characters, as in
targetString.assign( sourceString, start, numberOfCharacters );
where sourceString is the string
to be copied, start
is the starting subscript and numberOfCharacters
is the number of characters to copy.
1 // Fig. 19.1: Fig19_01.cpp
2 // Demonstrating string assignment and concatenation.
3 #include <iostream>
4 #include <string>
5 using namespace std;
6
7 int main()
8 {
9 string string1( "cat" );
10 string string2; // initialized to the empty string
11 string string3; // initialized to the empty string
12
13 string2 = string1; // assign string1 to string2
14 string3.assign( string1 ); // assign string1 to string3
15 cout << "string1: " << string1 << "
string2: " << string2
16 << "
string3: " << string3 << "
";
17
18 // modify string2 and string3
19 string2[ 0 ] = string3[ 2 ] = 'r';
20
21 cout << "After modification of string2 and string3:
" << "string1: "
22 << string1 << "
string2: " << string2 << "
string3: ";
23
24 // demonstrating member function at
25 for ( size_t i = 0; i < string3.size(); ++i )
26 cout << string3.at( i ); // can throw out_of_range exception
27
28 // declare string4 and string5
29 string string4( string1 + "apult" ); // concatenation
30 string string5; // initialized to the empty string
31
32 // overloaded +=
33 string3 += "pet"; // create "carpet"
34 string1.append( "acomb" ); // create "catacomb"
35
36 // append subscript locations 4 through end of string1 to
37 // create string "comb" (string5 was initially empty)
38 string5.append( string1, 4, string1.size() - 4 );
39
40 cout << "
After concatenation:
string1: " << string1
41 << "
string2: " << string2 << "
string3: " << string3
42 << "
string4: " << string4 << "
string5: " << string5 << endl;
43 } // end main
string1: cat
string2: cat
string3: cat
After modification of string2 and string3:
string1: cat
string2: rat
string3: car
After concatenation:
string1: catacomb
string2: rat
string3: carpet
string4: catapult
string5: comb
Line 19 uses the subscript operator to assign 'r'
to string3[ 2 ]
(forming "car"
) and to assign 'r'
to string2[ 0 ]
(forming "rat"
). The string
s are then output.
Lines 25–26 output the contents of string3
one character at a time using member function at. Member function at
provides checked access (or range checking); i.e., going past the end of the string
throws an out_of_range
exception. The subscript operator, [], does not provide checked access. This is consistent with its use on arrays. Note that you can also iterate through the characters in a string using C++11’s range-based for
as in
for ( char c : string3 )
cout << c;
which ensures that you do not access any elements outside the string
’s bounds.
Common Programming Error 19.1
Accessing an element beyond the size of the string using the subscript operator is an unreported logic error.
String string4
is declared (line 29) and initialized to the result of concatenating string1
and "apult"
using the overloaded + operator, which for class string
denotes concatenation. Line 33 uses the overloaded addition assignment operator, +=, to concatenate string3
and "pet"
. Line 34 uses member function append to concatenate string1
and "acomb"
.
Line 38 appends the string "comb"
to empty string string5
. This member function is passed the string
(string1
) to retrieve characters from, the starting subscript in the string
(4
) and the number of characters to append (the value returned by string1.size() - 4
).