【发布时间】:2015-09-17 22:58:09
【问题描述】:
我在医院管理系统中使用 C++ 中的 struct 来保存患者记录,并使用它来添加、列出、搜索和删除记录,例如,
struct details{
char first_name[20];
char last_name[20];
int age;
char gender[7];
int phone;
char doc_name[40];
};
details d[100];
void add()
{
counter++;
cout<<"\n\t\t\tRECORD NUMBER: "<<counter;
cout<<"\n\t\t\t1.FIRST NAME: ";
cin>>d[counter].first_name;
cout<<"\t\t\t2.LAST NAME: ";
cin>>d[counter].last_name;
cout<<"\t\t\t3.AGE: ";
cin>>d[counter].age;
cout<<"\t\t\t4.GENDER: ";
cin>>d[counter].gender;
cout<<"\t\t\t5.PHONE: ";
cin>>d[counter].phone;
cout<<"\t\t\t6.DOCTOR: ";
cin>>d[counter].doc_name;
char add_another;
cout<<"\n Add another record (Y/N)?: ";
cin>>add_another;
if(add_another == 'Y')
add();
else
menu();
}
我想知道使用 struct 的时间复杂度。使用链表或映射或向量会是保存记录的更好选择吗?
【问题讨论】:
-
这毫无意义。使用
struct(顺便说一句,如果您使用c++,为什么不使用class?),您正在定义您的复合数据类型。向量、映射、列表或任何将成为组织此数据类型记录的抽象容器。它将替换您在此处使用的简单静态 array (d[100]) ... 不是您的struct。 -
所以,不要误会,但在考虑算法复杂性等之前,请先了解您正在使用的语言,例如类型定义和包含已定义类型对象的容器有什么区别。
-
@FelixPalmen 为什么不上课?它涉及更多的打字。
-
@juanchopanza 当然,如果您知道它将永远保持“哑”数据对象,
struct很好。 -
@FelixPalmen 是的,我愿意。因为它没有区别。通常,如果大多数成员都是公开的,我会选择
struct。否则,class。最后只是编码约定。
标签: c++ struct time-complexity