c++ 将string 拷贝到字符串数组
时间: 2023-12-07 15:36:35 浏览: 93
可以通过以下两种方法将string拷贝到字符串数组中:
1. 直接使用string的成员函数c_str()将string转换为C风格的字符串,然后使用strcpy()函数将其拷贝到字符数组中。
2. 定义一个新的与string长度相等的字符数组,然后使用strcpy()函数将string拷贝到字符数组中。
下面是两种方法的示例代码:
```c++
// 方法一:使用c_str()函数和strcpy()函数
#include <iostream>
#include <cstring>
using namespace std;
int main() {
string str = "hello world";
char arr[20];
strcpy(arr, str.c_str());
cout << arr << endl;
return 0;
}
// 方法二:定义新的字符数组并使用strcpy()函数
#include <iostream>
#include <cstring>
using namespace std;
int main() {
string str = "hello world";
char arr[str.length() + 1];
strcpy(arr, str.c_str());
cout << arr << endl;
return 0;
}
```
相关问题
C++中字符串和字符数组的转化
在C++中,字符串和字符数组可以相互转化,常用的方法有以下几种:
1. 字符串转字符数组
可以使用strcpy()函数将字符串拷贝到字符数组中,例如:
```c++
#include <iostream>
#include <cstring>
using namespace std;
int main() {
string str = "Hello World";
char arr[20];
strcpy(arr, str.c_str());
cout << arr << endl;
return 0;
}
```
2. 字符数组转字符串
可以使用string类的构造函数将字符数组转化为字符串,例如:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
char arr[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '\0'};
string str(arr);
cout << str << endl;
return 0;
}
```
3. 字符串转字符指针
可以使用c_str()函数将字符串转化为字符指针,例如:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello World";
const char* ptr = str.c_str();
cout << ptr << endl;
return 0;
}
```
4. 字符指针转字符串
可以使用string类的构造函数将字符指针转化为字符串,例如:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
const char* ptr = "Hello World";
string str(ptr);
cout << str << endl;
return 0;
}
```
需要注意的是,字符数组转字符串时,字符数组必须以'\0'结尾。而且,字符串转字符指针时返回的是const char*类型的指针,因为字符串是不可修改的。
C++ char 拷贝到 string
在 C 语言中,可以使用 strcpy() 函数将字符数组(即 char 类型数组)拷贝到字符串(即 char 类型指针)中。其函数原型如下:
```c
char* strcpy(char* dest, const char* src);
```
其中,dest 表示目标字符串的指针,src 表示源字符数组的指针。函数的返回值为目标字符串的指针。
使用示例:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello, world!";
char str2[20];
strcpy(str2, str1);
printf("str1: %s\n", str1);
printf("str2: %s\n", str2);
return 0;
}
```
输出结果为:
```
str1: Hello, world!
str2: Hello, world!
```
在上面的示例中,我们首先定义了字符数组 str1 和字符串 str2。然后,使用 strcpy() 函数将 str1 中的数据拷贝到 str2 中,并最终输出两个字符串的值。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)