结构体
1.结构体定义
#include<iostream>
#include<string>
using namespace std;
struct student
{ //成员列表
string name;
int age;
int score;
}stu3;
int main()
{
//创建后分别赋值
struct student stu1;//关键字 struct 可以省略
stu1.name = "张三";
stu1.age = 18;
stu1.score = 98;
cout << stu1.name << "的年龄为" << stu1.age << " 分数为" << stu1.score << endl;
//括号内进行赋值
struct student stu2 = {"李四", 18 , 90 };
cout << stu2.name << "的年龄为" << stu2.age << " 分数为" << stu2.score << endl;
//以上直接创建
stu3.age = 17;
stu3.name = "马超";
stu3.score = 100;
cout << stu3.name << "的年龄为" << stu3.age << " 分数为" << stu3.score << endl;
//数组保存
student stu[3] =
{
{"zhangsan",22,99},
{"lisi",23,93},
{"wangwu",22,89}
};
stu[0].name = "zhaosi";//重新赋值
stu[0].age = 20;
stu[0].score = 95;
for (int i = 0; i < 3; i++)
{
cout << stu[i].name << "的年龄为" << stu[i].age << " 分数为" << stu[i].score << endl;
}
//结构体指针
student s = { "zhaoyun",24,100 };
student *p = &s;
cout << p->name << endl;
system("pause");
return 0;
}
2.结构体嵌套
#include<iostream>
#include<string>
using namespace std;
struct student
{ //成员列表
string name;
int age;
int score;
}stu3;
struct techer
{
int id;
string name;
int age;
struct student stuxx;
};
int main()
{
techer t;
t.id = 10317;
t.age = 29;
t.name = "king";
t.stuxx.age = 15;
t.stuxx.score = 80;
t.stuxx.name = "liu";
cout << "工号为" <<t.id << "所带学生的成绩为" << endl;
cout << t.stuxx.score << endl;
system("pause");
return 0;
}
2.结构体的值传递、地址传递、const
#include<iostream>
#include<string>
using namespace std;
//结构体做学生参数
struct student
{
string name;
int age;
int score;
};
//1.值传递
void printstu1(student s)
{
s.age = 100;
cout << s.name << " " << s.age << " " << s.score << endl;
}
//2.地址传递
//形参中用指针,只占用四个字节,而且不会复制新的副本出来
//为了防止数据被写,形参可以加上const
void printstu2(const student *s)
{
//s->age = 101;
cout << s->name << " " << s->age << " " << s->score << endl;
}
int main()
{
student s1;
s1.name = "zhangsan";
s1.age = 18;
s1.score = 88;
cout << s1.name << " " << s1.age << " " << s1.score << endl;
printstu1(s1);
cout << s1.name << " " << s1.age << " " << s1.score << endl;
printstu2(&s1);
cout << s1.name << " " << s1.age << " " << s1.score << endl;
system("pause");
return 0;
}
3.案例
案例1:数组定义三个老师各带五个学生,用函数进行信息赋值,最后打印。
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
struct student
{
string name;
int score;
};
struct techer
{
string name;
student stu[5];
};
void print(techer t[], int len)
{
string name_seed = "ABCDE";
for (int i = 0; i < len; i++)
{
t[i].name = "techer_";
t[i].name += name_seed[i];
for (int j = 0; j < 5; j++)
{
t[i].stu[j].name = "student_";
t[i].stu[j].name += name_seed[j];
int random = rand() % 61+40;//40~99
t[i].stu[j].score = random;
}
}
}
void printinfo(techer t[], int len)
{
for (int i = 0; i < len; i++)
{
cout << t[i].name <<"所带的学生:"<<endl;
for (int j = 0; j < 5; j++)
{
cout << "\t" << t[i].stu[j].name << "的分数为" << " ";
cout << t[i].stu[j].score << endl;
}
}
}
int main()
{
srand((unsigned int)time(NULL));
techer t[3];
int len = (sizeof(t)) / (sizeof(t[0]));
print(t, len);
printinfo(t, len);
system("pause");
return 0;
}
案例2:
#include<iostream>
#include<string>
using namespace std;
//英雄结构体
struct hero
{
//姓名
string name;
//年龄
int age;
//型别
string sex;
};
void bubbleSort(struct hero heroarr[],int len)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - 1 - i; j++)
if (heroarr[j].age>heroarr[j+1].age)
{
struct hero temp = heroarr[j];
heroarr[j] = heroarr[j+1];
heroarr[j + 1] = temp;
}
}
}
void print(struct hero heroarr[],int len)
{
for (int i = 0; i < len; i++)
{
cout << heroarr[i].name << " " << heroarr[i].age << " " << heroarr[i].sex << endl;
}
}
int main()
{
//1.涉及英雄的结构体
//2.创建数组排放英雄
struct hero heroarr[5] =
{
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",21,"男"},
{"赵云",20,"男"},
{"貂蝉",19,"男"},
};
cout << "NO排序结果为" << endl;
for (int i = 0; i < 5; i++)
{
cout << heroarr[i].name << " " << heroarr[i].age << " " << heroarr[i].sex << endl;
}
//3.数组排序
int len = ((sizeof(heroarr)) / (sizeof(heroarr[0])));
bubbleSort(heroarr, len);
cout << "排序结果为" << endl;
//4.将之后结果打印输出
print(heroarr,len);
system("pause");
return 0;
}