c + + learning record (7 structure)

7 structure

7.1 definition and use of structure

Structures are user-defined data types, allowing users to store different data types
Syntax: struct structure name {structure member list};
There are three ways to create variables through structures:

  • struct structure name variable name
  • struct structure name variable name = {member 1 value, member 2 value...}
  • Create variables as you define the structure
//Structure definition
struct student
{
	//Member list
	string name;  //full name
	int age;      //Age
	int score;    //fraction
}stu3; //Structure variable creation method 3: create variables when defining a structure


int main() {

	//Structure variable creation method 1: struct structure name variable name
	struct student stu1; //The struct keyword can be omitted

	stu1.name = "Zhang San";
	stu1.age = 18;
	stu1.score = 100;
	
	cout << "full name:" << stu1.name << " Age:" << stu1.age  << " fraction:" << stu1.score << endl;

	//Structure variable creation method 2: struct structure name variable name = {member 1 value, member 2 value...}
	struct student stu2 = { "Li Si",19,60 };

	cout << "full name:" << stu2.name << " Age:" << stu2.age  << " fraction:" << stu2.score << endl;


	stu3.name = "Wang Wu";
	stu3.age = 18;
	stu3.score = 80;
	

	cout << "full name:" << stu3.name << " Age:" << stu3.age  << " fraction:" << stu3.score << endl;

	system("pause");

	return 0;
}

Note:

  • 1: When defining a structure, the keyword is struct and cannot be omitted
  • 2: When creating structural variables, the keyword struct can be omitted
  • 3: Structural variables use operators to access members

7.2 structure array

Function: put the custom structure into the array for easy maintenance

Syntax: struct structure name array name [number of elements] = {{}, {},... {}}

7.3 structure pointer

Function: access the members in the structure through the pointer, and use the operator - > to access the structure properties through the structure pointer.

//Structure definition
struct student
{
	//Member list
	string name;  //full name
	int age;      //Age
	int score;    //fraction
};
int main() {
	struct student stu = { "Zhang San",18,100, };
	struct student * p = &stu;
	p->score = 80; //Members can be accessed through the pointer - > operator
	cout << "full name:" << p->name << " Age:" << p->age << " fraction:" << p->score << endl;
	system("pause");
	return 0;
}

7.4 nested structures

Action: a member in a structure can be another structure
eg: each teacher tutors a student. Record the structure of a student in the structure of a teacher

//Definition of student structure
struct student
{
	//Member list
	string name;  //full name
	int age;      //Age
	int score;    //fraction
};
//Definition of teacher structure
struct teacher
{
    //Member list
	int id; //Employee number
	string name;  //Teacher name
	int age;   //Teacher age
	struct student stu; //Substructure student
};
int main() {
	struct teacher t1;
	t1.id = 10000;
	t1.name = "Lao Wang";
	t1.age = 40;
	t1.stu.name = "Zhang San";
	t1.stu.age = 18;
	t1.stu.score = 100;
	cout << "Teacher employee No.: " << t1.id << " full name: " << t1.name << " Age: " << t1.age << endl;
	cout << "Name of trainee: " << t1.stu.name << " Age:" << t1.stu.age << " Test score: " << t1.stu.score << endl;
	system("pause");
	return 0;
}

7.5 function parameters of structure

Function: pass structure as parameter to function
There are two delivery methods:

  • pass by value
  • Address delivery
    If you do not want to modify the data in the main function, pass it by value, otherwise pass it by address
//Definition of student structure
struct student
{
	//Member list
	string name;  //full name
	int age;      //Age
	int score;    //fraction
};

//pass by value
void printStudent(student stu )
{
	stu.age = 28;
	cout << "Name in subfunction:" << stu.name << " Age: " << stu.age  << " fraction:" << stu.score << endl;
}

//Address delivery
void printStudent2(student *stu)
{
	stu->age = 28;
	cout << "Name in subfunction:" << stu->name << " Age: " << stu->age  << " fraction:" << stu->score << endl;
}

int main() {

	student stu = { "Zhang San",18,100};
	//pass by value
	printStudent(stu);
	cout << "Name in main function:" << stu.name << " Age: " << stu.age << " fraction:" << stu.score << endl;

	cout << endl;

	//Address delivery
	printStudent2(&stu);
	cout << "Name in main function:" << stu.name << " Age: " << stu.age  << " fraction:" << stu.score << endl;

	system("pause");

	return 0;
}

7.6 const used in structure

Function: use const to prevent misoperation

//Definition of student structure
struct student
{
	//Member list
	string name;  //full name
	int age;      //Age
	int score;    //fraction
};
//const usage scenario
void printStudent(const student *stu) //Add const to prevent misoperation in the function body. The constant pointer value cannot be changed. Use const to ensure that the content in the address remains unchanged.
{
	//stu->age = 100; // The operation failed because of the const modifier
	cout << "full name:" << stu->name << " Age:" << stu->age << " fraction:" << stu->score << endl;
}
int main() {
	student stu = { "Zhang San",18,100 };
	printStudent(&stu);
	system("pause");
	return 0;
}

7.7 structural case

7.7.1 case 1

Case description:

The school is working on the completion project. Each teacher leads 5 students, with a total of 3 teachers. The needs are as follows:

Design the structure of students and teachers. In the structure of teachers, there are teachers' names and an array of 5 students as members,
Students' members have names and test scores. Create an array to store 3 teachers, and assign values to each teacher and students through functions,
Finally, print out the teacher data and the student data brought by the teacher.

struct Student
{
	string name;
	int score;
};
struct Teacher
{
	string name;
	Student sArray[5];
};

void allocateSpace(Teacher tArray[] , int len)
{
	string tName = "teacher";
	string sName = "student";
	string nameSeed = "ABCDE";
	for (int i = 0; i < len; i++)
	{
		tArray[i].name = tName + nameSeed[i];
		
		for (int j = 0; j < 5; j++)
		{
			tArray[i].sArray[j].name = sName + nameSeed[j];
			tArray[i].sArray[j].score = rand() % 61 + 40;
		}
	}
}

void printTeachers(Teacher tArray[], int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << tArray[i].name << endl;
		for (int j = 0; j < 5; j++)
		{
			cout << "\t full name:" << tArray[i].sArray[j].name << " fraction:" << tArray[i].sArray[j].score << endl;
		}
	}
}

int main() {

	srand((unsigned int)time(NULL)); //Random number seed header file #include < CTime >

	Teacher tArray[3]; //Teacher array

	int len = sizeof(tArray) / sizeof(Teacher);

	allocateSpace(tArray, len); //Create data

	printTeachers(tArray, len); //print data
	
	system("pause");

	return 0;
}

7.7.2 case 2

Case description:

Design a hero structure, including member name, age and gender; Create a structure array, in which 5 heroes are stored.

Through the bubble sorting algorithm, the heroes in the array are sorted in ascending order according to their age, and the sorted results are finally printed.

The information of the five heroes is as follows:

		{"Liu Bei",23,"male"},
		{"Guan Yu",22,"male"},
		{"Fei Zhang",20,"male"},
		{"Zhao Yun",21,"male"},
		{"army officer's hat ornaments",19,"female"},
//Hero structure
struct hero
{
	string name;
	int age;
	string sex;
};
//Bubble sorting
void bubbleSort(hero arr[] , int len)
{
	for (int i = 0; i < len - 1; i++)
	{
		for (int j = 0; j < len - 1 - i; j++)
		{
			if (arr[j].age > arr[j + 1].age)
			{
				hero temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
}
//Print array
void printHeros(hero arr[], int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << "full name: " << arr[i].name << " Gender: " << arr[i].sex << " Age: " << arr[i].age << endl;
	}
}

int main() {

	struct hero arr[5] =
	{
		{"Liu Bei",23,"male"},
		{"Guan Yu",22,"male"},
		{"Fei Zhang",20,"male"},
		{"Zhao Yun",21,"male"},
		{"army officer's hat ornaments",19,"female"},
	};

	int len = sizeof(arr) / sizeof(hero); //Get the number of array elements

	bubbleSort(arr, len); //sort

	printHeros(arr, len); //Print

	system("pause");

	return 0;
}

Keywords: C++

Added by bigray on Wed, 29 Sep 2021 06:13:38 +0300