结构体数组
#include<iostream>
using namespace std;
#include<string>
struct Student
{
string name;
int age;
int score;
};
int main()
{
struct Student stuArray[3]=
{
{"张三",18,100},
{"李四",20,90},
{"王五",30,84}
};
stuArray[2].name = "赵六";
stuArray[2].age = 60;
stuArray[2].score = 55;
for (int i = 0; i < 3; i++)
{
cout << "姓名:" << stuArray[i].name << " 年龄:"
<< stuArray[i].age << " 分数:"
<< stuArray[i].score << endl;
}
system("pause");
return 0;
}
结构体指针
#include<iostream>
using namespace std;
#include<string>
struct student
{
string name;
int age;
int score;
};
int main()
{
student s = { "张三",20,100 };
student *p = &s;
cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;
system("pause");
return 0;
}
结构体嵌套结构体
#include<iostream>
using namespace std;
#include<string>
struct student
{
string name;
int age;
int score;
};
struct teacher
{
int id;
string name;
int age;
struct student stu;
};
int main()
{
teacher t;
t.id = 1000;
t.name = "老王";
t.age = 50;
t.stu.name = "小王";
t.stu.age = 20;
t.stu.score = 100;
cout << "老师编号:" << t.id << " 老师姓名:" << t.name
<< " 老师年龄:" << t.age << " 老师辅导学生姓名:" << t.stu.name
<< " 老师辅导学生年龄:" << t.stu.age << " 老师辅导学生分数:" << t.stu.score << endl;
system("pause");
return 0;
}
结构体做函数参数
#include<iostream>
using namespace std;
#include<string>
struct student
{
string name;
int age;
int score;
};
void printStudent1(student s)
{
s.age = 100;
cout << "子函数1中的 姓名:" << s.name << " 年龄:" << s.age << " 分数:"
<< s.score << endl;
};
void printStudent2(student *s)
{
s->age = 200;
cout << "子函数2中的 姓名:" << s->name << " 年龄:" << s->age << " 分数:"
<< s->score << endl;
};
int main()
{
student s;
s.name = "张三";
s.age = 20;
s.score = 95;
cout << "main函数中的 姓名:" << s.name << " 年龄:" << s.age
<< " 分数:" << s.score << endl;
printStudent1(s);
cout << "main函数中的 姓名:" << s.name << " 年龄:" << s.age
<< " 分数:" << s.score << endl;
printStudent2(&s);
cout << "main函数中的 姓名:" << s.name << " 年龄:" << s.age
<< " 分数:" << s.score << endl;
system("pause");
return 0;
}
结构体中const使用场景
#include<iostream>
using namespace std;
#include<string>
struct student
{
string name;
int age;
int score;
};
void printStudent(const student *s)
{
cout << "姓名:" << s-> name << " 年龄:" << s->age << "分数:" << s->score << endl;
}
int main()
{
student s = { "张三",20,100 };
printStudent(&s);
system("pause");
return 0;
}
结构体案例1
#include<iostream>
using namespace std;
#include<string>
#include<ctime>
struct Student
{
string sName;
int score;
};
struct Teacher
{
string tName;
Student sArray[5];
};
void allocatSpace(Teacher *tArray,int len)
{
string nameseed = "ABCDE";
for (int i = 0; i < len; i++)
{
tArray[i].tName = "teacher_";
tArray[i].tName += nameseed[i];
for (int j = 0; j < 5; j++)
{
tArray[i].sArray[j].sName = "student_";
tArray[i].sArray[j].sName += nameseed[j];
int random = rand() % 61 + 40;
tArray[i].sArray[j].score = random;
}
}
};
void printInfo(Teacher *tArray,int len)
{
for (int i = 0; i < len; i++)
{
cout <<"老师姓名:" << tArray[i].tName << endl;
for (int j = 0; j < 5; j++)
{
cout << "\t学生姓名:" << tArray[i].sArray[j].sName << " 学生分数:"
<< tArray[i].sArray[j].score << endl;
}
}
};
int main()
{
srand((unsigned int)time(NULL));
Teacher tArray[3];
int len = sizeof(tArray) / sizeof(tArray[0]);
allocatSpace(tArray, len);
printInfo(tArray, len);
system("pause");
return 0;
}
结构体案例2
#include<iostream>
using namespace std;
#include<string>
struct Hero
{
string name;
int age;
string sex;
};
void bubbleSort(Hero *heroArr, int len)
{
for (int i = 0; i < len-1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (heroArr[j].age > heroArr[j + 1].age)
{
Hero temp = heroArr[j];
heroArr[j] = heroArr[j + 1];
heroArr[j + 1] = temp;
}
}
}
}
void printArr(Hero *heroArr, int len)
{
for (int i = 0; i < len; i++)
{
cout << "姓名:" << heroArr[i].name << " 年龄:" << heroArr[i].age
<< " 性别:" << heroArr[i].sex << endl;
}
}
int main()
{
Hero heroArr[5]=
{
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",20,"男"},
{"赵云",21,"男"},
{"貂蝉",19,"女"}
};
int len = sizeof(heroArr) / sizeof(heroArr[0]);
cout << "排序前:" << endl;
for (int i = 0; i < len; i++)
{
cout << "姓名:" << heroArr[i].name<<" 年龄:"
<< heroArr[i].age <<" 性别:"<< heroArr[i].sex << endl;
}
bubbleSort(heroArr, len);
cout<<"排序后"<<endl;
printArr(heroArr, len);
system("pause");
return 0;
}