当前位置:网站首页>Structure array, pointer and function and application cases
Structure array, pointer and function and application cases
2022-07-02 21:34:00 【I want to watch the snow with you】
1、 Array of structs
effect : The function of structure array is to put the customized structure into the array for convenient maintenance
// Structure definition
struct sanguo// three countries
{
string name;// full name
int age; // Age
string sex; // Gender
};
int main()
{
// Array of structs
struct sanguo hero[5] = {
{" Liu bei ",23," male "},
{" Guan yu ",22," male "},
{" Zhang Fei ",20," male "},
{" zhaoyun ",21," male "},
{" The sable cicada ",19," Woman "},
};// initialization
system("pause");
return 0;
}
2、 Structure pointer
effect : Access members in the structure through pointers
Using operators -> You can access structure properties through structure pointers
#include<string>
#include<iostream>
using namespace std;
// Structure definition
struct student
{
string name;// full name
int age; // Age
int score; // fraction
};
int main()
{
// Create student structure variables
struct student stu={" Ningzi ",20,100};
// Pointer to a struct variable
struct student *p=&stu;
// Access variables in the structure through pointers
p->score=59;
// Output structure
cout<<" The student's name :"<<stu.name <<"\n"<<" Student age :"<<stu.age <<"\n"<<" Students' scores :"<<stu.score<<endl;
system("pause");
return 0;
}
We accessed the student's grades through the structure pointer and made modifications

It can be seen that through pointer access, we can change the student's grades from 100 Change it to 59( Sorry, ningzi )
Of course, we can also access other variables through the structure pointer
cout<<" The student's name :"<<p->name <<"\n"<<" Student age :"<<p->age <<"\n"<<" Students' scores :"<<p->score<<endl;
summary Structure pointers can be accessed through '->' Access structure members
Structure pointer accesses structure array
#include<string>
#include<iostream>
using namespace std;
// Define student structure
struct student
{
string name;// full name
int age; // Age
int score; // fraction
};
int main()
{
int i;
struct student stu[3];// Create structure
struct student *p=stu;// The array itself represents the address So don't add &
for(i=0;i<3;i++,*p++)
{
cin>>p->name>>p->age>>p->score;// Circular input structure array
}
for(i=0;i<3;i++)
{
// Cyclic output
cout<<stu[i].name<<"\n"<<stu[i].age<<"\n"<<stu[i].score<<endl;
}
system("pause");
return 0;
} 
3、 Structure nested structure
A member of a structure can be another structure
For example, a teacher takes a student , A teacher's structure records a student's structure
// Define student structure
struct student
{
string name;// full name
int score; // fraction
};
// Define the teacher structure
struct teacher
{
string name;
struct student stu;// The teacher takes a student
};How to access, how to access
struct teacher th;// Create structure
th.name=" Li lei ";
th.stu.name=" Zhang San ";
th.stu.score=88; We can also use structure pointers to access
struct teacher *p=&th;
p->name=" Li lei ";
p->stu.name=" Zhang San ";
p->stu.score=88; 4、 Structure is used as function parameter ( Value passed )
effect : Pass the structure as a parameter to the function
#include<string>
#include<iostream>
using namespace std;
// Define student structure
struct student
{
string name;// full name
int age; // Age
int score; // fraction
};
void outstruct(struct student q);// Structure output function
int main()
{
int i;
struct student stu={" Zhang San ",18,100};// Create structure // initialization
outstruct(stu);
return 0;
}
void outstruct(struct student q)
{
cout<<q.name<<endl;
cout<<q.age<<endl;
cout<<q.score<<endl;
} 
It should be noted that this transfer only passes the value of the structure Modifying parameters in a function does not affect the values in the main function
void outstruct(struct student q)
{
q.age=55;// Make... In a function age=55
cout<<q.name<<endl;
cout<<q.score<<endl;
cout<<"in outstruct: age= "<<q.age<<endl;
}
// stay main Function to do the following
outstruct(stu);
cout<<"in main: age= "<<stu.age;
It can be seen that main Medium age No change has been made
5、 Structure pointer as function parameter ( Address delivery )
#include<string>
#include<iostream>
using namespace std;
// Define student structure
struct student
{
string name;// full name
int age; // Age
int score; // fraction
};
void getstruct(struct student *q);// Input structure function
int main()
{
struct student stu;// Create structure
getstruct(&stu);
cout<<stu.name<<"\n"<<stu.age<<"\n"<<stu.score<<endl;// Output
return 0;
}
void getstruct(struct student *q)
{
cin>>q->name;
cin>>q->score;// Input
cin>>q->age;
}
It should be noted that this kind of transmission passes the address of the structure Changing parameters in a function will affect the values in the main function
6、 Structure case 1
Case description : A teacher in the school takes five students , There are three teachers in all , Design the structure of students and teachers The teacher structure includes the teacher's name and the structure of five students , The student structure includes names and test scores , Create an array to save three teachers , Assign values to teachers and students through functions , Output teacher data and student data through functions .
The complete code is as follows
#include<string>
#include<iostream>
using namespace std;
// Define student structure
struct student
{
string name;
int score;
};
// Define the teacher structure
struct teacher
{
string name;
struct student xs[5];// Contains an array of student structures (5)
}ls[3];// Create an array of teacher structures
void getstruct(struct teacher *tc);// Structure input function
void outstruct(struct teacher *q);// Structure output function
int main()
{
getstruct(ls);
outstruct(ls);
return 0;
}
void getstruct(struct teacher* tc)
{
int i,j;
for (i = 0; i < 3; i++, *tc++)
{
cin >> tc->name;
for (j = 0; j < 5; j++)
{
cin >> tc->xs[j].name;// loop
cin >> tc->xs[j].score;// Input
}
}
}
void outstruct(struct teacher *q)
{
int i, j;
cout<<endl;
for (i = 0; i < 3; i++)
{
cout << q->name<<endl;
for (j = 0; j < 5; j++)
{
cout << q->xs[j].name<<" ";// loop
cout << q->xs[j].score<<endl;// Output
}
}
}
Structure case 2
Case description :
Design the structure of a hero, including the name Age Gender , Create an array of structs , Five heroes are stored in the array , Through bubble sorting, the heroes in the array are sorted in ascending order according to their ages , Print the structure array after sorting .
Here are the five heroes
{" Liu bei ",23," male "},
{" Guan yu ",22," male "},
{" Zhang Fei ",20," male "},
{" zhaoyun ",21," male "},
{" The sable cicada ",19," Woman "},
The complete code is as follows :
#include<string>
#include<iostream>
using namespace std;
// Define the hero structure
struct sanguo
{
string name;
int age;
string sex;
};
int main()
{
int i,j;
// Create an array of hero structures
struct sanguo hero[5] = {
{" Liu bei ",23," male "},
{" Guan yu ",22," male "},
{" Zhang Fei ",20," male "},
{" zhaoyun ",21," male "},
{" The sable cicada ",19," Woman "},
};
struct sanguo alg;// Create intermediate structure variables
for (i = 0; i < 5; i++)
{
for(j=0;j<4;j++)
{
if (hero[j].age > hero[j + 1].age)
{
alg = hero[j];
hero[j] = hero[j + 1];// Bubble sort
hero[j + 1] = alg;
}
}
}
for(i=0;i<5;i++)
{
// Output structure array
cout << hero[i].name << hero[i].age << hero[i].sex << endl;
}
return 0;
}
边栏推荐
- Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of multi-channel signal conditioners in the global market in 2022
- It is said that this year gold three silver four has become gold one silver two..
- 2021 v+ Quanzhen internet global innovation and Entrepreneurship Challenge, one of the top ten audio and video scene innovation and application pioneers
- Construction and maintenance of business websites [4]
- Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of the inverted front fork of the global market in 2022
- The web version of xshell supports FTP connection and SFTP connection [detailed tutorial] continued from the previous article
- Research Report on the overall scale, major manufacturers, major regions, products and applications of metal oxide arresters in the global market in 2022
- Construction and maintenance of business websites [8]
- treevalue——Master Nested Data Like Tensor
- How does esrally perform simple custom performance tests?
猜你喜欢

Volvo's first MPV is exposed! Comfortable and safe, equipped with 2.0T plug-in mixing system, it is worth first-class

MySQL learning record (6)

Hot backup routing protocol (HSRP)

Internal/validators js:124 throw new ERR_ INVALID_ ARG_ Type (name, 'string', value) -- solution
![[shutter] the shutter plug-in is used in the shutter project (shutter plug-in management platform | search shutter plug-in | install shutter plug-in | use shutter plug-in)](/img/80/215499c66243d5a4453d8e6206c012.jpg)
[shutter] the shutter plug-in is used in the shutter project (shutter plug-in management platform | search shutter plug-in | install shutter plug-in | use shutter plug-in)

7. Build native development environment

How is LinkedList added?

MySQL learning record (5)

How does esrally perform simple custom performance tests?

Capacity expansion mechanism of ArrayList
随机推荐
China's log saw blade market trend report, technological innovation and market forecast
China's Micro SD market trend report, technology dynamic innovation and market forecast
Internet Explorer ignores cookies on some domains (cannot read or set cookies)
Welfare | Hupu isux11 Anniversary Edition is limited to hand sale!
2021 software security report: open source code, happiness and disaster depend on each other?
[12] the water of the waves is clear, which can wash my tassel. The water of the waves is muddy, which can wash my feet
MySQL learning record (3)
Research Report on micro gripper industry - market status analysis and development prospect prediction
Get weekday / day of week for datetime column of dataframe - get weekday / day of week for datetime column of dataframe
Accounting regulations and professional ethics [19]
Research Report on the overall scale, major manufacturers, major regions, products and applications of swivel chair gas springs in the global market in 2022
Research Report on market supply and demand and strategy of China's plastic trunking industry
~91 rotation
MySQL learning record (5)
Research Report on market supply and demand and strategy of Chinese garden equipment industry
Golang embeds variables in strings
[hands on deep learning]02 softmax regression
Import a large amount of data to redis in shell mode
Makefile: usage of control functions (error, warning, info)
Codeworks global round 19 (CF 1637) a ~ e problem solution