C++ 在堆区开辟string类型的数组
时间: 2023-12-19 13:04:23 浏览: 64
为了在堆区开辟string类型的数组,可以使用new运算符。下面是一个示例代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
int n = 5; // 数组大小
string* strArr = new string[n]; // 在堆区开辟string类型的数组
// 给数组赋值
for (int i = 0; i < n; i++) {
strArr[i] = "hello" + to_string(i);
}
// 输出数组元素
for (int i = 0; i < n; i++) {
cout << strArr[i] << endl;
}
// 释放内存
delete[] strArr;
return 0;
}
```
该代码使用new运算符在堆区开辟了一个大小为5的string类型的数组,然后给数组赋值并输出数组元素,最后释放内存。
相关问题
C++数组动态空间开辟,数组的内容为字符串
可以使用动态内存分配函数malloc()来开辟数组的空间,然后使用strcpy()函数将字符串复制到数组中。以下是示例代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char* str = "Hello, world!";
char* arr = (char*)malloc(strlen(str) + 1);
strcpy(arr, str);
printf("%s\n", arr);
free(arr);
return 0;
}
```
这段代码将字符串"Hello, world!"复制到动态分配的数组中,并打印出来。最后别忘了使用free()函数释放内存。
1)录入学生信息首先输入学生人数,然后根据学生人数开辟动态数组; 2)学生信息包括学号、姓名、性别、三门课成绩、总分;其中学号、姓名、 性别、三门课成绩是需要从键盘录入的,而总分是由程序计算的,而不 是用户自己算了再输入。 3)显示结果(学生信息)要以列表的形式显示,注意美观性; 4)要有必要的容错措施,比如输入各科分数要在0-100之间,输入错误给 用户提示,让其重新输入; c++ c++
好的,这是一个录入学生信息的程序,其中包括学生人数、学号、姓名、性别、三门课成绩以及总分。请注意输入的数据的合法性,如果输入不合法,程序会提示用户重新输入。
```c++
#include <iostream>
#include <string>
using namespace std;
struct student {
string id; // 学号
string name; // 姓名
string gender; // 性别
int score1; // 第一门课成绩
int score2; // 第二门课成绩
int score3; // 第三门课成绩
int total; // 总分
};
int main() {
int n; // 学生人数
cout << "请输入学生人数:";
cin >> n;
student *stu = new student[n]; // 动态数组
for (int i = 0; i < n; i++) {
cout << "请输入第" << i+1 << "个学生的信息:" << endl;
// 录入学号
cout << "学号:";
cin >> stu[i].id;
// 录入姓名
cout << "姓名:";
cin >> stu[i].name;
// 录入性别
cout << "性别:";
cin >> stu[i].gender;
// 录入第一门课成绩
do {
cout << "第一门课成绩:";
cin >> stu[i].score1;
if (stu[i].score1 < 0 || stu[i].score1 > 100) {
cout << "输入成绩不合法,请重新输入!" << endl;
}
} while (stu[i].score1 < 0 || stu[i].score1 > 100);
// 录入第二门课成绩
do {
cout << "第二门课成绩:";
cin >> stu[i].score2;
if (stu[i].score2 < 0 || stu[i].score2 > 100) {
cout << "输入成绩不合法,请重新输入!" << endl;
}
} while (stu[i].score2 < 0 || stu[i].score2 > 100);
// 录入第三门课成绩
do {
cout << "第三门课成绩:";
cin >> stu[i].score3;
if (stu[i].score3 < 0 || stu[i].score3 > 100) {
cout << "输入成绩不合法,请重新输入!" << endl;
}
} while (stu[i].score3 < 0 || stu[i].score3 > 100);
// 计算总分
stu[i].total = stu[i].score1 + stu[i].score2 + stu[i].score3;
}
// 显示学生信息
cout << "学生信息如下:" << endl;
cout << "学号\t姓名\t性别\t第一门课成绩\t第二门课成绩\t第三门课成绩\t总分" << endl;
for (int i = 0; i < n; i++) {
cout << stu[i].id << "\t" << stu[i].name << "\t" << stu[i].gender << "\t" << stu[i].score1 << "\t\t" << stu[i].score2 << "\t\t" << stu[i].score3 << "\t\t" << stu[i].total << endl;
}
delete[] stu; // 释放内存
return 0;
}
```
相关推荐
![h](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)