... "beginning end"};
 9     string string2{"middle "};
10     string string3{"12345678"};
11     string string4{"xx"};
12
13     cout << "Initial strings:\nstring1: " << string1
14        << "\nstring2: " << string2 << "\nstring3: " << string3
15        << "\nstring4: " << string4 << "\n\n";
16
17     // insert "middle" at location 10 in string1
18     string1.insert(10, string2);
19
20     // insert "xx" at location 3 in string3
21     string3.insert(3, string4, 0, string::npos);
22
23     cout << "Strings after insert:\nstring1: " << string1
24        << "\nstring2: " << string2 << "\nstring3: " << string3
25        << "\nstring4: " << string4 << endl;
26  }
 Initial strings: string1: beginning end string2: middle string3: 12345678 string4: xx Strings after insert: string1: beginning middle end string2: middle string3: ...

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.