我正在尝试创建一个结构,其中具有嵌套结构(子结构)。如何定义这样的子结构,以及如何从外部访问它?
这是我的结构:
struct Uniform
{
std::string color;
};
struct Team
{
std::string tname;
int poensHome;
int poensGuest;
Uniform teamUniform;
};
struct Player
{
std::string name;
std::string surname;
int goals;
Team* team;
};
这是应该将值读入团队的函数,我如何读取嵌套结构Uniform UniformTeam的值?
void read1(Team a[],int n)
{
int i;
for(i=0;i<n;i++)
{
cout<<endl<<"Name of the team:";
cin>>a[i].tname;
cout<<endl<<"Poens of the team (home):";
cin>>a[i].poensHome;
cout<<endl<<"Poens of the team(guest)";
cin>>a[i].poensGuest;
}
}
我是否通过以下方式阅读:
cout<<endl<<"color of the team uniform";
cin>>a[i].Uniform.teamUniform;
最佳答案
以与读取任何结构成员相同的方式进行读取,关于嵌套结构(除了嵌套)没有什么特别的。
cout<<endl<<"color of the team uniform";
cin>>a[i].teamUniform.color;
关于c++ - 如何在代码中读取嵌套结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60947741/