c++代码实现拼接字符串
在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/jf44 著作权归作者所有。请勿转载和采集!