1// appending to string
2#include <iostream>
3#include <string>
4
5int main ()
6{
7 std::string str;
8 std::string str2="Writing ";
9 std::string str3="print 10 and then 5 more";
10
11 // used in the same order as described above:
12 str.append(str2); // "Writing "
13 str.append(str3,6,3); // "10 "
14 str.append("dots are cool",5); // "dots "
15 str.append("here: "); // "here: "
16 str.append(10u,'.'); // ".........."
17 str.append(str3.begin()+8,str3.end()); // " and then 5 more"
18 str.append<int>(5,0x2E); // "....."
19
20 std::cout << str << '\n';
21 return 0;
22}
1// string::operator+=
2#include <iostream>
3#include <string>
4
5int main ()
6{
7 std::string name ("John");
8 std::string family ("Smith");
9 name += " K. "; // c-string
10 name += family; // string
11 name += '\n'; // character
12
13 std::cout << name;
14 return 0;
15}
1// appending to string
2#include <iostream>
3#include <string>
4
5int main ()
6{
7 // easy way
8 std::string str = "Hello";
9 std::string str2 = " World";
10 std::cout << str + str2 << std::endl;
11 return 0;
12}