C++结构体
2021/11/9 17:10:34
本文主要是介绍C++结构体,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
C++结构体
结构体的基本概念:
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
结构体的定义和使用
语法: struct 结构体名 {结构体成员列表};
通过结构体创建变量的三种方法:
- struct 结构体名 变量名
- struct 结构体名 变量名 = {成员1值, 成员2值…};
- 定义结构体时候顺便创建变量,不推荐
#include<iostream> #include<string> using namespace std; // 定义结构体 struct Student { String name; int age; int score; }; // }s3; //方法三 int main() { // 方法一 struct Student s1; // 创建结构体变量时,关键字struct可省略 // 给s1属性赋值,通过. 访问结构体变量中的属性 s1.name = "张三"; s1.age = 18; s1.score = 100; cout << "姓名" << s1.name << endl; cout << "年龄" << s1.age << endl; cout << "总分" << s1.score << endl; // 方法二 struct Student s2 = {"李四",19,80}; cout << "姓名" << s2.name << endl; cout << "年龄" << s2.age << endl; cout << "总分" << s2.score << endl; // 方法三,在创建结构体后的花括号外创建s3,再同方法一赋值 s3.name = "王五"; s3.age = 20; s3.score = 60; cout << "姓名" << s3.name << endl; cout << "年龄" << s3.age << endl; cout << "总分" << s3.score << endl; system("pause"); return 0; }
结构体数组
用于将自定义的结构体放入但数组中方便维护
语法:struct 结构体名 数组名[元素个数] = { {},{},{}…}
#include<iostream> #include<string> using namespace std; // 结构体定义 struct Student { string name; int age; int score; }; int main() { // 结构体数组 struct student arr[3] = { {"张三",18,80}, {"李四",19,60}, {"王五",20,60} }; // 给结构体数组中的元素赋值 arr[2].name = "赵六"; // 遍历结构体数组 for (int i = 0; i < 3; i++) { cout << "姓名:" << arr[i].name << endl; cout << "年龄:" << arr[i].age << endl; cout << "分数:" << arr[i].score << endl; } system("pause"); return 0; }
结构体指针
作用:通过指针访问结构体中的成员
- 利用操作符 -> 可以通过结构体指针访问结构体属性
#include<iostream> #include<string> using namespace std; struct Student { string name; int age; int score; }; int main() { student s = {"张三",18,100}; // 通过指针指向结构体变量 student* p = &s; // 通过指针访问结构体属性 cout << "姓名:" << p->name << endl; cout << "年龄:" << p->age << endl; cout << "分数:" << p->score << endl; system("pause"); 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; Student stu; }; int main() { // 创建老师及其学生 Teacher t; t.id = 100000; t.name = "老王"; t.age = 50; t.stu.name = "张三"; t.stu.age = 20; t.stu.score = 60; system("pause"); return 0; }
结构体做函数参数
将结构体作为参数向函数中传递
传递方式:
- 值传递
- 地址传递
#include<iostream> #include<string> using namespace std; struct Student { string name; int age; int score; }; // 方法一,一般用于不需要修改实参的情形 void printStudent(Student s) { cout << "print name:" << s.name << endl; cout << "print age:" << s.age << endl; cout << "print score:" << s.score << endl; } // 方法二,一般用于需要修改实参的情形 void printStudent2(Student* p) { cout << "p->name:" << p->name << endl; cout << "p->age:" << p->age << endl; cout << "p-score:" << p->score << endl; } int main() { Student s; s.name = "张三"; s.age = 18; s.score = 90; printStudent(s); printStudent2(&s); system("pause"); return 0; }
结构体中const使用场景
用const来防止误操作
#include<iostream> #include<string> using namespace std; struct Student { string name; int age; int score; }; // 使用地址传递可以节省内存空间(值传递会再复制一份实参过来) // 使用const可防止函数中结构体的属性值被修改 void printStudent(const Student* p) { // p->name = "李四"; // 报错 cout << "p->name" << p->name << endl; cout << "p->age" << p->age << endl; cout << "p-score" << p->score << endl; } int main() { Student s = {"张三",18,95}; printStudent(&s); system("pause"); return 0; }
这篇关于C++结构体的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-04el-table 开启定时器下,表格的选中状态会消失是什么原因-icode9专业技术文章分享
- 2024-10-03如何安装和初始化飞牛私有云 fnOS?-icode9专业技术文章分享
- 2024-10-03如何安装 App 并连接到飞牛 NAS?-icode9专业技术文章分享
- 2024-10-03如何安装飞牛 TV 并连接到影视服务器?-icode9专业技术文章分享
- 2024-10-03如何在PVE和ESXI上安装飞牛私有云 fnOS?-icode9专业技术文章分享
- 2024-10-03fnOS国产最强NAS安装系统异常情况处理-icode9专业技术文章分享
- 2024-10-03飞牛NAS如何创建存储空间?-icode9专业技术文章分享
- 2024-10-03fnOS国产最强NAS硬盘会自动休眠吗?-icode9专业技术文章分享
- 2024-10-03fnOS国产最强NAS如何安装飞牛影视和创建媒体库?-icode9专业技术文章分享
- 2024-10-03fnOS国产最强NAS如何为家人朋友开通影视账号?-icode9专业技术文章分享