当前位置:网站首页>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 applications of micro hydraulic cylinders in the global market in 2022
- Research Report on market supply and demand and strategy of China's plastic trunking industry
- Research Report on ranking analysis and investment strategic planning of RFID market competitiveness of China's industrial manufacturing 2022-2028 Edition
- 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 the overall scale, major manufacturers, major regions, products and applications of capacitive voltage transformers in the global market in 2022
- A river of spring water flows eastward
- Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of power management units in the global market in 2022
- Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of shock absorber oil in the global market in 2022
- How does esrally perform simple custom performance tests?
- Analysis of enterprise financial statements [4]
猜你喜欢
Customized Huawei hg8546m restores Huawei's original interface
Investment strategy analysis of China's electronic information manufacturing industry and forecast report on the demand outlook of the 14th five year plan 2022-2028 Edition
Capacity expansion mechanism of ArrayList
[shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)
treevalue——Master Nested Data Like Tensor
[error record] the command line creates an error pub get failed (server unavailable) -- attempting retry 1 in 1 second
In depth research and investment feasibility report of global and Chinese isolator industry, 2022-2028
[hands on deep learning]02 softmax regression
[shutter] statefulwidget component (image component | textfield component)
[CV] Wu Enda machine learning course notes | Chapter 12
随机推荐
I drew a Gu ailing with characters!
Research Report on the overall scale, major manufacturers, major regions, products and applications of building automation power meters in the global market in 2022
[shutter] shutter layout component (Introduction to layout component | row component | column component | sizedbox component | clipoval component)
Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of shock absorber oil in the global market in 2022
2021 v+ Quanzhen internet global innovation and Entrepreneurship Challenge, one of the top ten audio and video scene innovation and application pioneers
China Indonesia advanced wound care market trend report, technological innovation and market forecast
treevalue——Master Nested Data Like Tensor
[fluent] dart technique (independent main function entry | nullable type determination | default value setting)
Number of DP schemes
Construction and maintenance of business websites [9]
This team with billions of data access and open source dreams is waiting for you to join
D4:非成对图像去雾,基于密度与深度分解的自增强方法(CVPR 2022)
Lantern Festival, come and guess lantern riddles to win the "year of the tiger Doll"!
MySQL learning record (1)
China plastic box market trend report, technological innovation and market forecast
Research Report on ranking analysis and investment strategic planning of RFID market competitiveness of China's industrial manufacturing 2022-2028 Edition
Construction and maintenance of business website [3]
Construction and maintenance of business websites [7]
[shutter] statefulwidget component (pageview component)
[C language] [sword finger offer article] - replace spaces