1.结构特的定义与使用
#include <iostream>
#include <string>
using namespace std;
struct student
{
string name;
int age;
double score;
}s3;
int main()
{
//定义一
//struct student s1;
student s1; //c++中结构体在创建变量时关键字可以省但在定义是不能省
s1.age = 17;
s1.name = "张三";
s1.score = 100;
cout << "姓名:" << s1.name << "年龄:" << s1.age << "分数:" << s1.score << endl;
//定义二
struct student s2 = {"李四",19,80};
cout << "姓名:" << s2.name << "年龄:" << s2.age << "分数:" << s2.score << endl;
//定义三
s3.age = 18;
s3.name = "王五";
s3.score = 60;
cout << "姓名:" << s3.name << "年龄:" << s3.age << "分数:" << s3.score << endl;
system("pause");
return 0;
}
2.结构体数组
#include <iostream>
#include <string>
using namespace std;
struct student
{
string name;
int age;
double score;
};
int main()
{
int i;
struct student stuArray[3] = { {"张三",18,100 },{"李四",19,99},{"王五",20,101} };
stuArray[2].name = "赵六"; //把数组中的第三个元素替换了
stuArray[2].age = 18;
stuArray[2].score = 20;
for (i = 0; i < 3; i++)
{
cout << "姓名:" << stuArray[i].name << "年龄:" << stuArray[i].age << "分数:" << stuArray[i].score << endl;
}
system("pause");
return 0;
}
3.结构体指针
#include <iostream>
#include <string>
using namespace std;
struct student
{
string name;
int age;
double score;
};
int main()
{
struct student *p;
struct student s1;
s1.age = 18;
s1.name = "zhangsan";
s1.score = 123;
p = &s1;
cout << "姓名:" << s1.name << "年龄:" << s1.age << "分数:" << s1.score << endl;
cout << "姓名:" << p->name << "年龄:" << p->age << "分数:" << p->score << endl;
system("pause");
return 0;
}
4.结构体嵌套结构体
#include <iostream>
#include <string>
using namespace std;
struct student
{
int age;
string name;
double score;
};
struct teacher
{
int id;
string name;
int age;
struct student s1;
};
int main()
{
struct teacher t1;
t1.id = 1000;
t1.age = 34;
t1.name = "老王";
t1.s1.name = "小王";
t1.s1.age = 12;
t1.s1.score = 99;
cout << "老师的姓名: " << t1.name << " 老师的年龄: " << t1.age << " 老师的编号: " << t1.id << " 老师辅导的学生姓名: " << t1.s1.name << endl;
system("pause");
return 0;
}
5.结构体做函数参数
#include <iostream>
#include <string>
using namespace std;
struct student
{
int age;
string name;
double score;
};
//值传递
void printmsg1(struct student s)
{
s.age = 100;
cout << "子函数1中打印 姓名:" << s.name << " 年龄:" << s.age << " 考试分数: " << s.score << endl;
}
//地址传递
void printmsg2(struct student *s)
{
s->age = 100;
cout << "子函数2中打印 姓名:" << s->name << " 年龄:" << s->age << " 考试分数: " << s->score << endl;
}
int main()
{
struct student *p;
struct student s1;
s1.age = 19;
s1.name = "张三";
s1.score = 123;
p = &s1;
cout << "main函数中打印 姓名:" << s1.name << " 年龄:" << s1.age << " 考试分数: " << s1.score << endl;
printmsg1(s1);
cout << "子函数1改变年龄后为:" << s1.age << endl; //19实参改变失败
//printmsg2(&s1);
printmsg2(p);
cout << "子函数2改变年龄后为:" << s1.age << endl; //100实参改变成功
system("pause");
return 0;
}
6.结构体中const使用场景
#include <iostream>
#include <string>
using namespace std;
struct student
{
string name;
int age;
double score;
};
//将函数的形参改为指针,可以减少内存空间,而且不会复制新的副本出来
void printStudent(const struct student *s)
{
//s->age = 100; //加入const后,一旦有修改操作就会报错,可以防止我们误操作
cout << "姓名: " << s->name << " 年龄: " << s->age << " 分数: " << s->score << endl;
}
int main()
{
struct student s1 = {"张三",15,89.5};
printStudent(&s1);
cout << "姓名: " << s1.name << " 年龄: " << s1.age << " 分数: " << s1.score << endl;
system("pause");
return 0;
}
7.结构体项目
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
struct student
{
double score;
string name;
};
struct teacher
{
string tname;
struct student sarray[5];
};
void allocate(struct 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].name = "student_";
tarray[i].sarray[j].name += nameSeed[j];
int random = rand() % 61 + 40; //40-100的一个随机数
tarray[i].sarray[j].score = random;
}
}
}
void printInfo(struct 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].name <<
" 考试的分数: " << tarray[i].sarray[j].score << endl;
}
}
}
int main()
{
//随机数种子
srand((unsigned int)time(NULL));
struct teacher tarray[3];
allocate(tarray, sizeof(tarray) / sizeof(tarray[0]));
printInfo(tarray, sizeof(tarray) / sizeof(tarray[0]));
system("pause");
return 0;
}
8.结构体案例2
#include <iostream>
#include <string>
using namespace std;
//1.设计英雄的结构体
struct Hero
{
string name;
int age;
string sex;
};
void bubbleSort(struct Hero harray[],int len)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - i - 1 ;j++)
{
if (harray[j].age > harray[j + 1].age)
{
struct Hero temp = harray[j];
harray[j] = harray[j + 1];
harray[j + 1] = temp;
}
}
}
}
void printHero(struct Hero harray[], int len)
{
for (int i = 0; i < len; i++)
{
cout << "姓名: " << harray[i].name << "年龄: " << harray[i].age << "性别" << harray[i].sex << endl;
}
}
int main()
{
//2.创建数组存放五名英雄
struct Hero harray[5] = { {"刘备",23,"男"},{"关羽",22,"男"},{"张飞",21,"男"},{"赵云",20,"男"},{"貂蝉",19,"男"} };
int len = sizeof(harray) / sizeof(harray[0]);
for (int i = 0; i < len; i++)
{
cout << "姓名: " << harray[i].name << "年龄: " << harray[i].age << "性别" << harray[i].sex << endl;
}
//3.对数据进行排序,按年龄进行升序排序
bubbleSort(harray,len);
//4.打印排序后的结果
cout << "排序后的打印结果" << endl;
printHero(harray,len);
system("pause");
return 0;
}