... }; i < string3.size(); ++i) {
25         cout << string3.at(i); // can throw out_of_range exception
26      }
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 << "\n\nAfter concatenation:\nstring1: " << string1
41         << "\nstring2: " << string2 << "\nstring3: " << string3
42         << "\nstring4: " << string4 << "\nstring5: " << string5 << endl;
43  }
 string1: cat string2: cat ...

Get C++ How to Program, 10/e now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.