当前位置:网站首页>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;
}
边栏推荐
- Don't you want to have a face-to-face communication with cloud native and open source experts? (including benefits
- Construction and maintenance of business websites [8]
- Research Report on the overall scale, major manufacturers, major regions, products and applications of hollow porcelain insulators in the global market in 2022
- Lantern Festival, come and guess lantern riddles to win the "year of the tiger Doll"!
- Research Report on the overall scale, major manufacturers, major regions, products and applications of swivel chair gas springs in the global market in 2022
- Customized Huawei hg8546m restores Huawei's original interface
- MySQL learning record (3)
- Market trend report, technical dynamic innovation and market forecast of China's low gloss instrument
- Internal/validators js:124 throw new ERR_ INVALID_ ARG_ Type (name, 'string', value) -- solution
- Research Report on market supply and demand and strategy of China's atomic spectrometer industry
猜你喜欢
Share the easy-to-use fastadmin open source system - Installation
Welfare, let me introduce you to someone
MySQL learning record (2)
[shutter] statefulwidget component (image component | textfield component)
6 pyspark Library
[CV] Wu Enda machine learning course notes | Chapter 12
[fluent] dart technique (independent main function entry | nullable type determination | default value setting)
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 (4)
[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)
随机推荐
Spend more time with your computer on this special holiday, HHH
kernel tty_ struct
Internet Explorer ignores cookies on some domains (cannot read or set cookies)
Write the content into the picture with type or echo and view it with WinHex
China Indonesia advanced wound care market trend report, technological innovation and market forecast
[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
[CV] Wu Enda machine learning course notes | Chapter 12
Accounting regulations and professional ethics [17]
Research Report on the overall scale, major manufacturers, major regions, products and applications of outdoor vacuum circuit breakers in the global market in 2022
Research Report on crude oil tanker industry - market status analysis and development prospect forecast
Accounting regulations and professional ethics [19]
Common authority query instructions in Oracle
如何防止你的 jar 被反编译?
暑期第一周总结
[dynamic planning] p1220: interval DP: turn off the street lights
Sword finger offer (I) -- handwriting singleton mode
[shutter] shutter layout component (opacity component | clipprect component | padding component)
Plastic floating dock Industry Research Report - market status analysis and development prospect forecast
One week dynamics of dragon lizard community | 2.07-2.13
Analysis of enterprise financial statements [4]