#include#include int main() { std::string str1 = "hello"; std::string str2 = " world"; str1.append(str2); std::cout << str1 << std::endl; // Output: "hello world" return 0; }
#include#include int main() { std::string str1 = "hello"; char str2[] = " world"; str1.append(str2); std::cout << str1 << std::endl; // Output: "hello world" return 0; }
#includeThis example shows how you can use "append" to concatenate a substring (a portion of an existing string) to an existing std::string. In this case, we use the "substr" method to extract the substring "world" from "str1", and then use "append" to add it back to the end of "str1". The "std::string append" method is part of the C++ standard library, which is included in the "std" namespace. To use it, you simply need to include the#include int main() { std::string str1 = "hello world"; std::string str2 = str1.substr(6, 5); // "world" str1.append(str2); std::cout << str1 << std::endl; // Output: "hello worldworld" return 0; }