主要有两种:
使用C++中的std::string类
std::string类提供了一系列操作字符串的方法,其中最常用的是append()函数,可以用来拼接字符串。
// 定义字符串 std::string str1 = "Hello"; std::string str2 = "World"; // 使用append()函数拼接字符串 str1.append(str2); // 输出结果 std::cout << str1 << std::endl; // 输出:HelloWorld
使用C++标准库提供的操作字符串的函数
C++标准库提供了一系列操作字符串的函数,其中最常用的是strcat()函数,可以用来拼接字符串。
// 定义字符串 char str1[20] = "Hello"; char str2[20] = "World"; // 使用strcat()函数拼接字符串 strcat(str1, str2); // 输出结果 std::cout << str1 << std::endl; // 输出:HelloWorld
:主要有两种,一种是使用std::string类提供的append()函数,另一种是使用C++标准库提供的strcat()函数。