在C++中,可以使用+运算符或者string类的成员函数append()来实现字符串的拼接。下面是两种实现方式的示例代码:

  1. 使用+运算符拼接字符串:
#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
  1. 使用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
c++代码实现拼接字符串

原文地址: https://www.cveoy.top/t/topic/jf44 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录