C++ 字符串拼接:使用 + 运算符和 append() 函数
在 C++ 中,可以使用 ' + ' 运算符或者 'string' 类的成员函数 'append()' 来实现字符串的拼接。下面是两种实现方式的示例代码:
- 使用 ' + ' 运算符拼接字符串:
#include <iostream>
#include <string>
int main() {
std::string str1 = 'Hello';
std::string str2 = 'World';
std::string result = str1 + ' ' + str2;
std::cout << result << std::endl;
return 0;
}
输出结果为:
Hello World
- 使用 'append()' 函数拼接字符串:
#include <iostream>
#include <string>
int main() {
std::string str1 = 'Hello';
std::string str2 = 'World';
str1.append(' ');
str1.append(str2);
std::cout << str1 << std::endl;
return 0;
}
输出结果为:
Hello World
原文地址: https://www.cveoy.top/t/topic/da4r 著作权归作者所有。请勿转载和采集!