#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
struct student
{
string m_name;
int m_sex;
int m_age;
};
struct adminstudent
{
struct student** data;
int m_size;
};
void add_person(struct adminstudent* p)
{
int n;
string name;
int sex, age;
cout << "你想添加的人數" << endl;
cin >> n;
int newsize = n + p->m_size;
struct student** newspace = NULL;
newspace = new struct student* [newsize];
for (int i = 0; i < p->m_size; i++)
{
newspace[i] = p->data[i];
}
for (int i = p->m_size; i < newsize; i++)
{
newspace[i] = NULL;
newspace[i] = new struct student();
cout << "性別" << endl;
cin >> sex;
newspace[i]->m_sex = sex;
cout << "名字" << endl;
cin >> name;
newspace[i]->m_name = name;
cout << "年齡" << endl;
cin >> age;
newspace[i]->m_age = age;
delete newspace[i];
}
delete[] p->data;
p->data = newspace;
delete[] newspace;
p->m_size = newsize;
}
void show_person(struct adminstudent* p)
{
int i;
for( i = 0; i < p->m_size; i++)
{
cout << p->data[i]->m_name << endl;
cout << p->data[i]->m_sex << endl;
cout << p->data[i]->m_age << endl;
}
}
int main()
{
struct adminstudent* p = new struct adminstudent();
p->m_size = 0;
p->data = NULL;
add_person(p);
show_person(p);
delete p;
}
uj5u.com熱心網友回復:
洗掉下面兩行:delete newspace[i];
delete[] newspace;
uj5u.com熱心網友回復:
能解釋一下為什么第二個要刪掉嗎

,大佬救命
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/268375.html
標籤:C++ 語言
