阅读量:132
在C++中,可以使用string类的append()方法或者+运算符来拼接字符串。下面分别演示了两种方法:
- 使用
append()方法拼接字符串:
#include
#include
int main() {
std::string str1 = "Hello, ";
std::string str2 = "world!";
str1.append(str2);
std::cout << str1 class="hljs-keyword">return 0;
}
- 使用
+运算符拼接字符串:
#include
#include
int main() {
std::string str1 = "Hello, ";
std::string str2 = "world!";
std::string result = str1 + str2;
std::cout << result class="hljs-keyword">return 0;
}
以上两种方法都可以用来拼接字符串,选择其中一种方法即可。