【发布时间】:2019-01-15 16:30:09
【问题描述】:
我想删除在学生结构下声明的数组元素。我只包含了部分代码以减少混淆。请在下面找到两个相关案例的代码:
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
struct student
{
char name[20];
int id;
};
struct teacher
{
char name[20];
int id;
};
int main()
{
case 1:
cout<<"\t\t\t*********enter record**********"<<endl;
student st[2];
for(int count=0; count<2;count++)
{
cout<<"\t\t\t\tenter student "<<count<<" name"<<endl;
cin>>st[count].name;
cout<<"\t\t\t\tenter student "<<count<<" id"<<endl;
cin>>st[count].id;
}
break;
case 5:
cout<<"\t\t\t*********delete record********"<<endl;
for(int count=0;count<10;count++)
{
delete st[count].name;
}
break;
}
如案例 5 所示,我正在尝试使用 delete st[count].name; 删除数组中的元素;
我想在delete的情况下删除name和id这两个元素。但是使用 delete st[count].name 会给我一个 [Warning] Deleting array 。当我运行程序时,它会给我一个程序收到信号 SIGTRAP、跟踪/断点陷阱。我是 C++ 的初学者,请帮助我如何删除存储在这些数组中的元素。谢谢
【问题讨论】:
-
你可以
delete只能是new动态分配的东西,这不是你的情况。 -
那里没有可删除的内容。它是一个 char 数组,其内存分配在结构中,不能单独释放。还有为什么要使用聊天数组而不是
std::string? -
我想在案例 1 中删除我正在写入 student st[2] 的数据。我想在案例 5 中删除它
-
你说的“删除数据”是什么意思?
-
使用
memset。