C++| Ingenious work from 0 to 1 introductory programming [video + courseware + notes + source code]
table of Contents
8.1 Basic concepts of structure
8.2 Structure definition and use
8.5 Structure Nested Structure
8.6 Structure as function parameter
8.7 Scenarios for using const in the structure
8 structure
8.1 Basic concepts of structure
The structure is a user -defined data type , allowing users to store different data types.
C++ built-in data types: int, float, double, string, char...
8.2 Structure definition and use
grammar:
struct 结构体名 { 结构体成员列表 };
There are 3 ways to create variables through structures :
struct structure name variable name
struct structure name variable name = {member 1 value, member 2 value...}
When defining the structure, create variables by the way
Example
#include <iostream>
#include <string>
using namespace std;
//1、创建学生数据类型:学生包括(姓名、年龄、分数)
//自定义数据类型,一些数据类型集合组成的一个类型(其实就是内置数据类型的集合)。
//语法:struct 类型名称 { 成员列表 }
//结构体定义
struct student //结构体名称-见名知意
{
//成员列表---学生属性
string name; //姓名
int age; //年龄
int score; //分数
} stu3; //结构体变量创建方式3
//2、通过学生类型创建具体学生(共有3种方式)
//2.1、struct student s1; // s1就是变量名
//2.2、struct student s2 = {...}; // 创建变量的同时赋初值
//2.3、在定义结构体时顺便创建结构体变量
int main()
{
//【结构体变量创建方式1】2.1、struct student s1;
struct student stu1; //创建结构体时,struct关键字可以省略
//给stu1属性赋值,通过“.”访问结构体变量中的属性
stu1.name = "张三";
stu1.age = 18;
stu1.score = 100;
//方式1打印输出
cout << "姓名:" << stu1.name << " 年龄:" << stu1.age << " 分数:" << stu1.score << endl;
//【结构体变量创建方式2】2.2、struct student s2 = {...};
struct student stu2 = {"李四", 19, 60};
//方式2打印输出
cout << "姓名:" << stu2.name << " 年龄:" << stu2.age << " 分数:" << stu2.score << endl;
//【结构体变量创建方式3】在定义结构体时顺便创建结构体变量,不建议使用
stu3.name = "王五";
stu3.age = 18;
stu3.score = 80;
//方式3打印输出
cout << "姓名:" << stu3.name << " 年龄:" << stu3.age << " 分数:" << stu3.score << endl;
system("pause");
return 0;
}
- Summary 1: The keyword when defining a structure is struct, which cannot be omitted.
- Summary 2: When creating structure variables, the keyword struct can be omitted.
- Summary 3: Structure variables use the operator ``.'' to access members.
8.3 Structure array
Function: Put the custom structure into the array to facilitate maintenance .
grammar:
struct 结构体名 数组名[元素个数] = { {} , {} , ... {} }
Example
#include <iostream>
#include <string>
using namespace std;
//结构体数组
//1、定义结构体
struct student
{
//成员列表
string name; //姓名
int age; //年龄
int score; //分数
};
int main()
{
//2、创建结构体数组
struct student stuArray[3] =
{
{"张三", 18, 88},
{"李四", 28, 99},
{"王五", 38, 66}
};
//3、给结构体数组中的元素赋值
stuArray[2].name = "赵六";
stuArray[2].age = 66;
stuArray[2].score = 99;
//4、遍历结构体数组
for (int i = 0; i < 3; i++)
{
cout << "姓名:" << stuArray[i].name
<< "\t年龄:" << stuArray[i].age
<< "\t分数:" << stuArray[i].score << endl;
}
system("pause");
return 0;
}
8.4 Structure pointer
Role: Access the members of the structure through the pointer . // Use structure pointers to manipulate structure members .
Using the operator
->
can structure pointer access structure properties .
Example
&s: Take the address of s and return the pointer of the data type corresponding to s.
#include <iostream>
#include <string>
using namespace std;
//结构体指针
//1、定义学生结构体
struct student
{ //成员列表
string name; //姓名
int age; //年龄
int score; //分数
};
int main()
{
//2、创建学生结构体变量
// struct student stu = {"张三", 25, 100}; //struct可以省略
student stu = {"张三", 25, 99}; //struct可以省略
//3、通过指针指向结构体变量
// struct student *p = &stu; //struct可以省略
student *p = &stu; //struct可以省略
//结构体变量,可以直接通过.来访问
cout << "姓名:" << stu.name << "\t年龄:" << stu.age << "\t分数:" << stu.score << endl;
//4、通过指针访问结构体变量中的数据
p->score = 66; //指针通过 -> 操作符可以访问成员
//通过结构体指针访问结构体中的属性,需要利用“->”
cout << "姓名:" << p->name << "\t年龄:" << p->age << "\t分数:" << p->score << endl;
system("pause");
return 0;
}
- Summary: The structure pointer can access the members of the structure through the -> operator.
8.5 Structure Nested Structure
Role: A member of a structure can be another structure .
For example: each teacher tutors a student, and a teacher’s structure records a student’s structure.
Example
#include <iostream>
#include <string>
using namespace std;
// 结构体嵌套结构体
//定义学生结构体
struct student
{
//成员列表
string name; //姓名
int age; //年龄
int score; //分数
};
//定义教师结构体
struct teacher
{
//成员列表
int id; //教师编号
string name; //教师姓名
int age; //教师年龄
struct student stu; //教师辅导的学生 子结构体
};
int main()
{
//创建教师结构体变量
struct teacher t1;
t1.id = 333;
t1.name = "老王";
t1.age = 50;
t1.stu.name = "张三";
t1.stu.age = 18;
t1.stu.score = 99;
cout << "教师编号:" << t1.id
<< "\t教师姓名:" << t1.name
<< "\t教师年龄:" << t1.age
<< "\t教师辅导的学生姓名:" << t1.stu.name
<< endl;
cout << "学生姓名:" << t1.stu.name
<< "\t学生年龄:" << t1.stu.age
<< "\t学生考试分数:" << t1.stu.score
<< endl;
system("pause");
return 0;
}
Summary: In a structure, you can define another structure as a member to solve practical problems.
8.6 Structure as function parameter
Role: Pass the structure as a parameter to the function.
There are two ways of delivery:
Pass by value
Address pass
Example
#include <iostream>
#include <string>
using namespace std;
//定义学生结构体
struct student
{
//成员列表
string name; //姓名
int age; //年龄
int score; //分数
};
//打印学生信息函数
//1、值传递
void printStudent1(struct student stu) //(student stu)
{
stu.age = 28; //修改属性
cout << "1子函数中\t姓名:" << stu.name << "\t年龄:" << stu.age << "\t分数:" << stu.score << endl;
}
//2、地址传递
void printStudent2(struct student *stu) //用指针*stu接收地址
{
stu->age = 28;
cout << "2子函数中\t姓名:" << stu->name << "\t年龄:" << stu->age << "\t分数:" << stu->score << endl;
}
int main()
{
//结构体作函数参数
//将学生传入到一个参数中,打印学生身上的所有信息
//创建结构体变量
student stu = {"张三", 18, 100};
stu.name = "李四";
stu.age = 20;
stu.score = 85;
//1、值传递
printStudent1(stu);
cout << "1主函数中\t姓名:" << stu.name << "\t年龄:" << stu.age << "\t分数:" << stu.score << endl;
cout << endl;
//2、地址传递
printStudent2(&stu);
cout << "2主函数中\t姓名:" << stu.name << "\t年龄:" << stu.age << "\t分数:" << stu.score << endl;
system("pause");
return 0;
}
Summary: If you don't want to modify the data in the main function, pass it by value, otherwise pass it by address.
8.7 Scenarios for using const in the structure
Role: Use const to prevent misoperation .
Example
#include <iostream>
#include <string>
using namespace std;
//学生结构体定义
struct student
{
//成员列表
string name; //姓名
int age; //年龄
int score; //分数
};
/*
* 值传递
* stu.age = 150;//mian中的stu变量不会改变
* 会将实参中的数据拷贝一份,放在形参s上
* 无论形参如何改变,都不会影响实参
* 1、实参有很多属性,每个属性都拷贝一份,拷贝的数据量很大
* 2、假设一个学校有成千上万个人,每个人都调用printStudent函数,拷贝成千上万份数据,
* 这样数据量就会非常大,占用内存空间大
*/
void printStudent1(student stu) // (struct student stu) 省略struct
{
stu.age = 150; //mian中的stu变量不会改变
cout << "姓名:" << stu.name << "\t年龄:" << stu.age << "\t分数:" << stu.score << endl;
}
// void printStudent3(const student stu) // (struct student stu) 省略struct
// {
// stu.age = 150; //mian中的stu变量不会改变
// cout << "姓名:" << stu.name << "\t年龄:" << stu.age << "\t分数:" << stu.score << endl;
// }
//将函数中的形参改为指针,可以减少内存空间的使用,而且不会复制新的副本出来(值传递会复制整个结构体元素)
//const使用场景
void printStudent2(const student *stu) //加const防止函数体中的误操作 指针*stu节省空间,一个指针占4个字节内存
{
//stu->age = 100; //操作失败,因为加了const修饰,常量指针无法修改指针指向的值,只能读不能写。防止age的值会被修改
//加入const之后,一旦有修改的操作就会报错,可以防止我们的误操作(加“const”防止误操作)
cout << "姓名:" << stu->name << "\t年龄:" << stu->age << "\t分数:" << stu->score << endl;
}
int main()
{
//创建结构体变量
student stu = {"张三", 18, 100};
printStudent1(stu); //值传递
//通过函数打印结构体变量信息
printStudent2(&stu); //传入地址,在函数中指针接收地址
cout << "main()中 张三的年龄为:" << stu.age << endl;
system("pause");
return 0;
}
8.8 Structure case
8.8.1 Case 1
Case description:
The school is doing a final project. Each teacher leads 5 students, and there are 3 teachers in total. The requirements are as follows:
- Design the structure of students and teachers. In the structure of the teacher, there are the teacher's name and an array of 5 students as members;
- Student members have names and test scores, create an array to store 3 teachers, and assign values to each teacher and the students they bring through functions;
- Finally print out the teacher data and the student data brought by the teacher.
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
//学生结构体
struct Student
{
string sName; //姓名
int score; //分数
};
//老师结构体
struct Teacher
{
string tName; //姓名
struct Student sArray[5]; //学生数组
};
//创建数据——给老师和学生赋值的函数
void allocateSpace(struct Teacher tArray[], int len) //struct可以省略
{
string tName = "老师"; // Teacher_
string sName = "学生";
string nameSeed = "ABCDE";
//给老师进行赋值
for (int i = 0; i < len; i++)
{
tArray[i].tName = tName + nameSeed[i]; //老师姓名
//通过循环给每名老师所带的学生进行赋值
for (int j = 0; j < 5; j++)
{
tArray[i].sArray[j].sName = sName + nameSeed[j];
tArray[i].sArray[j].score = rand() % 61 + 40; // 0~60 40~100
// rand() % 60:0~59
}
}
}
//打印数据——打印所有信息
void printTeachers(struct Teacher tArray[], int len) //struct可以省略
{
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)); //随机数种子 头文件 #include <ctime>
//1、创建3名老师的数组
struct Teacher tArray[3]; //老师数组 struct可以省略
//2、通过函数给3名老师的信息赋值,并给老师带的学生信息赋值
cout << "string类型所占内存空间为:" << sizeof(string) << endl; //string类型所占内存空间为:32
cout << "int类型所占内存空间为:" << sizeof(int) << endl; //int类型所占内存空间为:4
cout << "sizeof(tArray):" << sizeof(tArray) << endl; //sizeof(tArray):696
cout << "sizeof(Teacher):" << sizeof(Teacher) << endl; //sizeof(Teacher):232
cout << "sizeof(Student):" << sizeof(Student) << endl; //sizeof(Student):40
cout << "sizeof(tArray[0]):" << sizeof(tArray[0]) << endl; //sizeof(tArray[0]):232
int len1 = sizeof(tArray) / sizeof(Teacher); //计算数组长度
cout << "len1:" << len1 << endl; //len1:3
int len2 = sizeof(tArray) / sizeof(tArray[0]); //计算数组长度
cout << "len2:" << len2 << endl; //len2:3
allocateSpace(tArray, len2); //创建数据
//3、打印所有老师及所带的学生信息
printTeachers(tArray, len2); //打印数据
system("pause");
return 0;
}
8.8.2 Case 2
Case description:
Design a hero structure, including member names, ages, and genders; create a structure array, and store 5 heroes in the array.
Through the bubble sort algorithm , the heroes in the array are sorted in ascending order by age , and the sorted result is finally printed.
The information of the five heroes is as follows:
{"Liu Bei", 23, "Male"},
{"Guan Yu", 22, "Male"},
{"Zhang Fei", 20, "Male"},
{"Zhao Yun", 21, "Male"},
{ "Diao Chan", 19, "Female"},
#include <iostream>
#include <string>
using namespace std;
//1、设计英雄结构体
//英雄结构体
struct hero
{
string name; //姓名
int age; //年龄
string sex; //性别
};
//冒泡排序——实现年龄升序排列
void bubbleSort(struct hero arr[], int len) //struct可省
{
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - 1 - i; j++)
{
//如果j下标的元素年龄大于j+1下标的元素的年龄,交换两个元素
if (arr[j].age > arr[j + 1].age)
{
struct hero temp = arr[j]; //临时数据
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
//打印数组——打印排序后数组中的信息
void printHeros(struct hero arr[], int len) //struct可省
{
for (int i = 0; i < len; i++)
{
cout << "姓名:" << arr[i].name << "\t性别:" << arr[i].sex << "\t年龄:" << arr[i].age << endl;
}
}
//1、设计英雄结构体
//2、创建数组存放5名英雄
//3、对数组进行排序,按照年龄进行升序排序
//4、将排序后的结果打印输出
int main()
{
//2、创建数组存放5名英雄
struct hero heroArray[5] =
{
{"刘备", 23, "男"},
{"关羽", 22, "男"},
{"张飞", 20, "男"},
{"赵云", 21, "男"},
{"貂蝉", 19, "女"}};
cout << "string类型所占内存空间为:" << sizeof(string) << endl; //string类型所占内存空间为:32
cout << "int类型所占内存空间为:" << sizeof(int) << endl; //int类型所占内存空间为:4
cout << "sizeof(heroArray):" << sizeof(heroArray) << endl; //sizeof(heroArray):360
cout << "sizeof(heroArray[0]):" << sizeof(heroArray[0]) << endl; //sizeof(heroArray[0]):72
cout << "sizeof(hero):" << sizeof(hero) << endl; //sizeof(hero):72
int len1 = sizeof(heroArray) / sizeof(heroArray[0]); //整体所占空间大小/单个元素所占空间大小
cout << "len1:" << len1 << endl; //len1:5
int len2 = sizeof(heroArray) / sizeof(hero); //获取数组元素个数
cout << "len2:" << len2 << endl; //len1:5
cout << "排序前,数组打印:" << endl;
for (int i = 0; i < len1; i++)
{
cout << "姓名:" << heroArray[i].name << "\t性别:" << heroArray[i].sex << "\t年龄:" << heroArray[i].age << endl;
}
//3、对数组进行排序,按照年龄进行升序排序
bubbleSort(heroArray, len1); //排序
cout << "排序后,数组打印:" << endl;
//4、将排序后的结果打印输出
printHeros(heroArray, len1); //打印
system("pause");
return 0;
}
3 points of this case exercise:
- How to write structure array
- Bubble sort // Use "bubble sort" to sort the structure array
- The writing method of structure incoming function, the writing method of operation structure