Code前端首页关于Code前端联系我们

使用C++结构实现数据的分组和管理

terry 2年前 (2023-10-01) 阅读数 135 #c++
文章标签 mysql cte

一、结构体定义和使用

在C++中,结构体可以用于存储不同类型的数据,在一个数据结构中组合起来。比如,我们可以定义一个结构体来存储学生的姓名、年龄和成绩:

struct Student {
    string name;
    int age;
    double score;
};

我们可以通过定义一个结构体变量来存储一个学生的信息:

Student stu1;
stu1.name = "Tom";
stu1.age = 18;
stu1.score = 95.5;

结构体也可以用数组的方式来创建多个结构体变量:

Student stu_list[3];
stu_list[0].name = "Tom";
stu_list[0].age = 18;
stu_list[0].score = 95.5;
stu_list[1].name = "Jerry";
stu_list[1].age = 19;
stu_list[1].score = 87.5;
stu_list[2].name = "Lisa";
stu_list[2].age = 20;
stu_list[2].score = 90.0;

我们可以通过遍历数组来输出所有结构体变量:

for(int i=0; i

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门