P64 结构体-结构体定义和使用
P65 结构体-结构体数组
P66 结构体-结构体 指针
P67 结构体-结构体嵌套结构体
P68 结构体-结构体做函数参数
P69 结构体-结构体中const使用场景
P70 结构体-结构体案例1
P71 结构体-结构体案例2
1 、结构体-结构体定义和使用
//结构体基本概念
//结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
//结构体的定义和使用
//语法:struct 结构体名{结构体成员列表};
//通过结构体创建变量的方式有三种
//
//struct 结构体名 变量名
//struct 结构体名 变量名 = {成员1值,成员2值}
//创建结构体时,顺便创建变量
//第一种和第二种用的比较多,第三种用的比较少,第三种不用学了
#include<iostream>
using namespace std;
#include<string> //要用C++的字符串,只需要包含头文件就可以啦。
//结构体定义
//创建学生数据类型:学生包括(姓名,年龄,分数)
//自定义数据类型,一些类型集合组成的一个类型
//语法:struct /不能省略。
struct student{string name;int age;int score;}; // 结构体定义的时候,
//关键字struct不可以省略,但是下面创建结构体的时候,关键字可以省略。
//2、通过学生类型创建具体学生
int main()
{
//2.1struct student s1
struct student s1;
//给s1属性赋值,通过.访问结构体变量中的属性。
s1.name = "张三 ";
s1.age = 18;
s1.score = 100;
cout << "姓名 :" << s1.name << " 年龄 :" << s1.age << " 分数 : " << s1.score << endl;
//2.2struct student s2 = {...}顺便把初值给出来。
struct student s2 = { "张三 ", 18, 100 };
cout << "姓名 :" << s2.name << " 年龄 :" << s2.age << " 分数 : " << s2.score << endl;
//2.3在定义结构体时,顺便创建结构体变量
system("pause");
return 0;
}
2、结构体-结构体数组
可以换行操作。
#include<iostream>
using namespace std;
#include<string>
//1、定义结构体数组
struct student
{
string name ;
int age ;
int score ;
};
int main()
{
//2、创建结构体数组 ——定义结构体数组的同时,就可以给他赋值。
struct student stuArray[3]=
{
{"张三",20,80},
{"李四",30,70},
{"王五",30,90},
};
//3、给结构体数组中的元素赋值
stuArray[2].name = "赵六";
stuArray[2].age = 80;
stuArray[2].score = 60;
//4、遍历结构体数组
for (int i = 0; i < 3; i++)
{
cout << " 姓名: " << stuArray[i].name << " 年龄:" << stuArray[i].age << " 分数 :" << stuArray[i].score << endl;
}
system("pause");
return 0;
}
3、结构体-结构体 指针
#include<iostream>
using namespace std;
#include<string>
struct student { string name; int age; int score; };
int main()
{
student stu = { "姓名", 20,90 };
student* p = &stu;
cout << "姓名:" << p->name<< "年龄: " << p->age << "分数: " << p->score << endl;
system("pause");
return 0;
}
4、结构体-结构体嵌套结构体
另一个结构体是本结构体中的成员 。
总结:一个结构体嵌套另一个结构体,可以解决一些实际问题。例如上边。
#include<iostream>
using namespace std;
#include<string>
struct student { string name; int age; int score; };
//struct teacher { struct stu1; struct stu2; struct stu3; };
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 = 90;
cout << t.id << t.name << t.age << t.stu.name << t.stu.age << t.stu.score << endl;
system("pause");
return 0;
}
5、结构体-结构体做函数参数
C++的学习暂时告一段路——20230801
C++的学习继续赶路——20230820 (20号上午,先复习了一下上边的内容,以及一些指针的内容,下午继续学习新东西。)
问题解决发现:
代码中执行顺序不一样,可能会有不一样的结果。
这个情况下,形参会传递给实参。
这个情况下,形参不会传递给实参。
6、结构体-结构体中const使用场景
代码示例。
#include<iostream>
using namespace std;
#include<string>
// const的使用场景
//将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本出来。
//加上const后,只能读,不能写,所以,不会出现误操作,不小心把形参给改了的情况。
struct student
{
string name;
int age;
int score;
};
void printStudent(const student* stu) //加上const,防止函数体中的误操作。
{
//stu->age = 100; //操作失败,因为加了const修饰。
cout << "姓名 : " << stu->name << "年龄: " << stu->age << " 分数: " << stu->score << endl;
}
int main()
{
struct student stu = { "张三",25,96};
printStudent(&stu);
system("pause");
return 0;
}
//不管你的数据量有多少,我的指针只占用4个字节,用指针来提高代码的运行效率,在这体现出来了。改成指针,可以节省空间。
//如果是值传递,数据多大,传的就会有多大,完全复制出一个副本,
//加入const后,里面一旦出现误操作,编译器就会报错。
//加入const后,限制一个只读状态,防止出现误操作。
7、 结构体-结构体案例1
#include<iostream>
using namespace std;
#include<string> //如果有打印的字符串输出,需要包含这个头文件。
#include<ctime> //使用系统时间所需要包含的头文件。
//学生的结构体定义
struct student
{
string sName;
int score;
};
//老师的结构体定义
struct Teacher //每个老师下面两个属性。
{
string tName;
struct student sArray[5];
};
//给老师和学生赋值的函数
//PS:不需要返回值的,使用void函数类型。
void allocateSpace(struct Teacher tArray[],int len) //allocateSpace 相当于给老师的赋值开辟空间。 后边括号,传入数组,以及数组的长度。
{
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; //40~100 新知识。哦哦哦,之前讲过,哈哈,%60,取模于60,数值是0到59.???
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].sName << // \t,之前学的转义字符,空出来的缩进更加明显。
" 考试分数 : " << tArray[i].sArray[j].score << endl;
}
}
}
int main()
{
//随机数种子
srand((unsigned int)time(NULL)); //time,系统时间
//1、创建三名老师的数组
struct Teacher tArray[3]; //三个老师放在一个数组中。
//2、通过函数给3名老师的信息赋值并给老师所带的学生信息赋值
int len = sizeof(tArray) / sizeof(tArray[0]);
allocateSpace(tArray, len);
//3、打印所有老师及所带的学生信息
printInfo(tArray, len);
system("pause");
return 0;
}
//1、总结:大小写字母是一定要注意的
//本案例涉及到数组,函数,结构体,
//
// 小结:再看了一遍。
//
//
//在学习本案例的时候,是很难得,也没有理解的特别通透,花了差不多一天的时间。
错误经验总结:
我又单独写了一遍,后边运行过程中的问题是:我只改了07源文件中的main——main7,但没有改其他两个函数,导致运行错误。无法编译。所以,自己定义的函数也需要进行区别,不只是main函数。
8、结构体-结构体案例2
必要的阶段性的测试一定要有,写一小部分的测试代码,可以提高编程效率。
对冒泡排序,内外循环的理解,内循环,负责调换两个数的顺序,外循环负责轮数。
#include<iostream>
using namespace std;
#include<string>
//定义结构体
struct hero
{
string name;
int age;
string gender;
};
void bubbleSort(struct hero array[], int len)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (array [j].age > array[j + 1].age)
{
struct hero temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
void printhero(struct hero array[], int len)
{
for (int i = 0; i < len; i++)
{
cout << "姓名: " << array[i].name << " 年龄: " << array[i].age << " 性别 : " << array[i].gender << endl;
}
}
int main()
{
struct hero array[5] =
{
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",25,"男"},
{"赵云",26,"男"},
{"貂蝉",20,"女"},
};
cout << "排序前的结果: " << endl;
int len = sizeof(array) / sizeof(array[0]);
bubbleSort(array, len);
printhero(array, len);
system("pause");
return 0;
}
//hero的,实参和形参不兼容,是什么问题?? 大小写有误。
//结构体数组可以做出来,但是冒泡排序比较难,没思路。
//
//for (int i = 0; i < len; i++)
//{
// cout << "姓名: " << array[i].name << " 年龄: " << array[i].age << " 性别 : " << array[i].gender << endl;
//}
1、结构体数组
2,冒泡排序一定要会,
3,参数是如何传入结构体中的。
20230821晚记录