c++字符串连接
时间: 2023-08-03 08:14:20 浏览: 40
C++中可以使用运算符+或者函数append()来进行字符串连接。
使用运算符+:
```cpp
std::string str1 = "Hello";
std::string str2 = "World";
std::string str3 = str1 + str2;
```
使用函数append():
```cpp
std::string str1 = "Hello";
std::string str2 = "World";
str1.append(str2);
```
相关问题
c++ 字符串连接符合
C语言中的字符串连接可以通过使用字符串连接运算符 "+" 来实现。例如,我们可以将两个字符串 "Hello" 和 "World" 连接起来,代码如下所示:
```
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
char result[100];
strcpy(result, str1);
strcat(result, str2);
printf("连接后的字符串为: %s\n", result);
return 0;
}
```
运行结果为:连接后的字符串为: HelloWorld
c++字符串连接string函数
C++中的字符串连接函数是`std::string`类的成员函数`append()`或`+=`操作符。这些函数可以用于将一个字符串连接到另一个字符串的末尾。
使用`append()`函数,可以将一个字符串连接到另一个字符串的末尾。例如:
```cpp
std::string str1 = "Hello";
std::string str2 = " World";
str1.append(str2); // 将str2连接到str1的末尾
```
使用`+=`操作符,也可以实现相同的效果:
```cpp
std::string str1 = "Hello";
std::string str2 = " World";
str1 += str2; // 将str2连接到str1的末尾
```
以上两种方法都会将`str2`连接到`str1`的末尾,最终结果为`"Hello World"`。