当前位置: 首页 > news >正文

C++笔记-14-结构体

结构体定义和使用

语法:struct 结构体名 {结构体成员列表};
通过结构体创建变量的方式有三种:

  1. struct 结构体名 变量名
  2. struct 结构体名 变量名={成员1值,成员2值…}
  3. 定义结构体时顺便创建变量

第一种方式创建结构体

#include<iostream>
#include<string>
using namespace std;
struct student {string name;int age;int score;
};
int main() {struct student s1;s1.name = "张三";s1.age = 20;s1.score = 90;cout << "姓名:" << s1.name << endl;cout << "年龄:" << s1.age << endl;cout << "成绩:" << s1.score << endl;return 0;
}

第二种定义方式

#include<iostream>
#include<string>
using namespace std;
struct student {string name;int age;int score;
};
int main() {struct student s1 = { "zhangsan",18,90 };cout << "name:" << s1.name << endl;cout << "age:" << s1.age << endl;cout << "score:" << s1.score << endl;return 0;
}

第三种创建方式

#include<iostream>
#include<string>
using namespace std;
struct student {string name;int age;int score;
}s1;
int main() {s1.name = "李四";s1.age = 23;s1.score = 88;cout << "姓名" << s1.name << endl;cout << "年龄" << s1.age << endl;cout << "成绩" << s1.score << endl;return 0;
}

结构体在创建的时候struct可以省略,但是定义的时候不可以省略

结构体数组

语法:struct 结构体名 数组名[元素个数]={ {},{}.{}...{} }

#include<iostream>
#include<string>
using namespace std;
struct student {string name;int age;int score;
};
int main() {student stu[3] = { {"张三",18,90},{"李四",19,85},{"王五",17,95} };for(int i = 0; i < 3; i++) {cout << "姓名:" << stu[i].name << "\t年龄:" << stu[i].age << "\t成绩:" << stu[i].score << endl;}return 0;
}

结构体指针

结构体指针通过->来访问结构体里面的元素.

#include<iostream>
#include<string>
using namespace std;
struct student {string name;int age;int score;
};
int main() {student s1 = { "张三",20,90 };student* ps = &s1;cout << "姓名:" << ps->name << endl;cout << "年龄:" << ps->age << endl;cout << "成绩:" << ps->score << endl;return 0;
}

结构体嵌套结构体

#include<iostream>
#include<string>
using namespace std;
struct student {string name;int age;int score;
};
struct teacher {int id;string name;int age;struct student stu;
};
int main() {teacher t1 = { 1001, "Mr. Smith", 40, {"Alice", 20, 90} };cout << t1.id << endl;cout << t1.name << endl;cout << t1.age << endl;cout << t1.stu.name << endl;cout << t1.stu.age << endl;cout << t1.stu.score << endl;return 0;
}

结构体作为函数参数

  • 值传递
  • 地址传递
#include<iostream>
#include<string>
using namespace std;
struct student {string name;int age;int score;
};
//值传递
void print_student(student stu) {cout << "name: " << stu.name << endl;cout << "age: " << stu.age << endl;cout << "score: " << stu.score << endl;
}
//地址传递
void print_student(student* ps) {cout << "name: " << ps->name << endl;cout << "age: " << ps->age << endl;cout << "score: " << ps->score << endl;
}
int main() {student stu1 = { "张三", 20, 90 };print_student(stu1);student* ps = &stu1;print_student(ps);return 0;
}

结构体中const的使用场景

使用const防止误操作

#include<iostream>
#include<string>
using namespace std;
struct student {string name;int age;int score;
};
// 将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本
void print_student(const student* ps) {//ps->age = 30; 在加入const之后,不能修改指针所指向的内容cout << "name: " << ps->name << endl;cout << "age: " << ps->age << endl;cout << "score: " << ps->score << endl;
}
int main() {student stu1 = { "张三", 20, 90 };student* ps = &stu1;print_student(ps);return  0;
}

在函数中使用地址传递可以节省内存空间,再加上const就可以防止我们误操作修改结构体.

http://www.dtcms.com/a/561066.html

相关文章:

  • .NET周刊【10月第3期 2025-10-19】
  • 视频时间基 (time_base) 详解:时间的“刻度单位”
  • 网站开发最佳实践wordpress连接公众号
  • 数据库-基础命令
  • 蚌埠做企业网站wordpress外网访问不了
  • Linux网络接口配置:静态IP与动态IP设置(附代码示例)
  • 做同城特价的网站qwins是哪个网站做的
  • 基础算法精讲 03 | 滑动窗口|ASCII表如如何使用|substr函数
  • 中国建设银行官网首页 网站首页网站文件目录结构
  • GitHub Actions for AI:构建企业级模型CI/CD流水线
  • DevOps——CI/CD持续集成与持续交付/部署的理解与部署
  • 建立网站的公司平台七牛云存储 wordpress连接失败
  • 利用DeepSeek辅助修改luadbi-duckdb读取DuckDB decimal数据类型
  • 深圳网站设计网站制作深圳网站建设推进
  • 电力电子技术 第十三章——PWM逆变器
  • 网站建设方案应该怎么写wordpress用户评论图片
  • xtuoj 2021
  • 数据科学每日总结--Day8--数据挖掘
  • 达梦DEM监控工具部署
  • 机器学习实践项目(二)- 房价预测 - 认识数据
  • 李宁运动服网站建设规划书网站内链少改怎么做
  • 安装JDK安装GIT安装IDEA
  • 定制报表系统设计与实现
  • 最具价值的网站建设商业策划公司十大公司
  • 网站开发设计公东莞智通人才招聘网官网
  • 【BFS 解决FloodFill 算法】3. 岛屿的最⼤⾯积(medium)
  • 【JUnit实战3_23】 第十四章:JUnit 5 扩展模型(Extension API)实战(上)
  • python_study--week3
  • 【Excalidraw】简洁好看的超轻量级画图白板
  • 手写Autosar架构的CAN通讯协议栈2(CanIf模块详解-上)