目录
---------------------------------------------------------------------------------------------------------------------------------
今天我整理了一些struct的用法,但我先说一声:
我终于回来啦!
前几天一直没空,因为今天有空,所以今天更新一下。那么首先第一个用法是:
1,可以创造根据结构体格式的成员或数组。
普通成员
我们先看下列例子:
#include <stdio.h>
#include<iostream>
struct student
{
char name[20];
int age;
char sex[7];
int score;
};student stu1 = {"Wang.x.m",12,"boy",90};
int main(){
std::cout<<stu1.name<<" "<<stu1.age<<" "<<stu1.sex<<" "<<stu1.score;
}
这个代码很简单,首先创造一个经典结构体struct,然后在结构体里创建了变量name,age,sex和,score,之后创建一个结构体成员,最后打印。
运行结果:
数组成员
但是如果成员有10个,那么根据前面的就只能这么写:
#include <stdio.h>
#include <iostream>
struct student
{
char name[20];
int age;
char sex[7];
int score;
};
int main()
{
student stu1 = {"Wang.x.m", 12, "boy", 90};
student stu2 = {"Li.y.n", 13, "girl", 85};
student stu3 = {"Zhang.z.o", 11, "boy", 92};
student stu4 = {"Chen.a.p", 14, "boy", 88};
student stu5 = {"Liu.b.q", 12, "girl", 91};
student stu6 = {"Sun.c.r", 13, "boy", 87};
student stu7 = {"Zhao.d.s", 11, "girl", 93};
student stu8 = {"Wu.e.t", 14, "boy", 86};
student stu9 = {"Zhou.f.u", 12, "girl", 94};
student stu10 = {"Qian.g.v", 13, "boy", 89};
std::cout << stu1.name << " " << stu1.age << " " << stu1.sex << " " << stu1.score << std::endl;
std::cout << stu2.name << " " << stu2.age << " " << stu2.sex << " " << stu2.score << std::endl;
std::cout << stu3.name << " " << stu3.age << " " << stu3.sex << " " << stu3.score << std::endl;
std::cout << stu4.name << " " << stu4.age << " " << stu4.sex << " " << stu4.score << std::endl;
std::cout << stu5.name << " " << stu5.age << " " << stu5.sex << " " << stu5.score << std::endl;
std::cout << stu6.name << " " << stu6.age << " " << stu6.sex << " " << stu6.score << std::endl;
std::cout << stu7.name << " " << stu7.age << " " << stu7.sex << " " << stu7.score << std::endl;
std::cout << stu8.name << " " << stu8.age << " " << stu8.sex << " " << stu8.score << std::endl;
std::cout << stu9.name << " " << stu9.age << " " << stu9.sex << " " << stu9.score << std::endl;
std::cout << stu10.name << " " << stu10.age << " " << stu10.sex << " " << stu10.score << std::endl;
return 0;
}
这种代码十分不整洁,既不好看还效率低,这时候我们可以运用数组:
#include <stdio.h>
#include <iostream>
struct student
{
char name[20];
int age;
char sex[7];
int score;
};
int main()
{
student students[10] = {
{"Wang.x.m", 12, "boy", 90},
{"Li.y.n", 13, "girl", 85},
{"Zhang.z.o", 11, "boy", 92},
{"Chen.a.p", 14, "boy", 88},
{"Liu.b.q", 12, "girl", 91},
{"Sun.c.r", 13, "boy", 87},
{"Zhao.d.s", 11, "girl", 93},
{"Wu.e.t", 14, "boy", 86},
{"Zhou.f.u", 12, "girl", 94},
{"Qian.g.v", 13, "boy", 89}
};
for (int i = 0; i < 10; i++)
{
std::cout << students[i].name << " " << students[i].age << " " << students[i].sex << " " << students[i].score << std::endl;
}
return 0;
}
这样就比上一个少了900多个字符。
2,可以用指针遍历成员
但是这样需要0.58秒才能运行,很容易让我们等死,如果你想让运行得快一点,那么可以运用指针:
#include <stdio.h>
#include <iostream>
struct student
{
char name[20];
int age;
char sex[7];
int score;
};
int main()
{
student students[10] = {
{"Wang.x.m", 12, "boy", 90},
{"Li.y.n", 13, "girl", 85},
{"Zhang.z.o", 11, "boy", 92},
{"Chen.a.p", 14, "boy", 88},
{"Liu.b.q", 12, "girl", 91},
{"Sun.c.r", 13, "boy", 87},
{"Zhao.d.s", 11, "girl", 93},
{"Wu.e.t", 14, "boy", 86},
{"Zhou.f.u", 12, "girl", 94},
{"Qian.g.v", 13, "boy", 89}
};
student* ptr = students;
for (int i = 0; i < 10; i++)
{
std::cout << ptr->name << ptr->age << ptr->sex << ptr->score;
ptr++;
}
return 0;
}
3,使用typedef
但这样比之前也快了0.05秒。有没有更快的方法呢?有,使用typedef关键词,比如这样:
#include <stdio.h>
struct student
{
char name[20];
int age;
char sex[7];
int score;
};
typedef student Stu;
int main()
{
Stu students[10] = {
{"Wang.x.m", 12, "boy", 90},
{"Li.y.n", 13, "girl", 85},
{"Zhang.z.o", 11, "boy", 92},
{"Chen.a.p", 14, "boy", 88},
{"Liu.b.q", 12, "girl", 91},
{"Sun.c.r", 13, "boy", 87},
{"Zhao.d.s", 11, "girl", 93},
{"Wu.e.t", 14, "boy", 86},
{"Zhou.f.u", 12, "girl", 94},
{"Qian.g.v", 13, "boy", 89}
};
Stu* ptr = students;
for (int i = 0; i < 10; i++)
{
printf("%s %d %s %d\n", ptr->name, ptr->age, ptr->sex, ptr->score);
ptr++;
}
return 0;
}
这样写可以直接把运行速度飙升到:
4.格式环节
结构体创造:
struct 结构体名{
格式1
格式2
格式3
.
.
.
格式n
};
成员创造:
普通成员:
struct 结构体名 结构体成员名 = {结构体变量1,结构体变量2,结构体变量3・・・结构体变量n};
数组成员:
struct 结构体名 结构体成员[n]={{结构体变量1.1,结构体变量1.2,结构体变量1.3···结构体成员1.n},
{结构体变量2.1,结构体变量2.2,结构体变量2.3···结构体成员2.n},
{结构体变量3.1,结构体变量3.2,结构体变量3.3···结构体成员3.n},
.
.
.
{结构体变量n.1,结构体变量n.2,结构体变量n.3···结构体成员n.n}};
5.出题环节
最后该大家出点题:
【1,输出平均数】
请使用struct计算输入的n个平均数。
样例
输入:
5
6.7 8.2 1.3 2.9 7.9
输出:
20.68
【2, 输出总成绩】
请使用 struct 计算输入的 n 个学生成绩的总成绩。
样例:
输入:
3
85 90 88
输出:
263
【3, 输出最高分】
请使用 struct 找出输入的 n 个学生成绩中的最高分。
样例:
输入:
4
78 89 92 84
输出:
92
如果有什么没讲的话,我会尽快更新《sturct的用法第2期》
标签:精确,boy,12,struct,int,用法,student,girl From: https://blog.csdn.net/sidso/article/details/142433992