百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程网 > 正文

C++笔记:struct结构体 c++结构体使用

yuyutoo 2024-10-12 00:45 12 浏览 0 评论

8.1 结构体基本概念

结构体属于用户自定义的数据类型,允许用户存储不同的数据类型

8.2 结构体的定义与使用

struct 结构体名 {结构成员列表};

  • struct 结构体名 变量名
 #include<iostream>
 using namespace std;
 #include<string>
 //struct 类型名称 {成员列表}
 struct Student
 {
     //成员具体信息
     string name;
     int age;
     int score;
 };
 //通过学生类型创建具体学生
 
 int main()  
 {
     struct Student s1;
     //给s属性赋值,访问结构体变量中的属性
     s1.name = "张三";
     s1.age = 18;
     s1.score = 100;
     cout << "姓名:" << s1.name << endl;
     cout << "年龄:" << s1.age << endl;
     cout << "分数:" << s1.score << endl;
     return 0;
 }
  • struct 结构体名 变量名 = {成员1值,成员2值}
 #include<iostream>
 using namespace std;
 #include<string>
 //struct 类型名称 {成员列表}
 struct Student
 {
     //成员具体信息
     string name;
     int age;
     int score;
 }s1;
 //创建时顺便创建结构体变量
 //通过学生类型创建具体学生
 int main()
 {
     struct Student s1 = { "李四",19,110 };//struct 可省略,
     cout << "姓名:" << s1.name << endl;
     cout << "年龄:" << s1.age << endl;
     cout << "分数:" << s1.score << endl;
     return 0;
 };
  • 定义结构体时顺便创建变量(不推荐)
 #include<iostream>
 using namespace std;
 #include<string>
 //struct 类型名称 {成员列表}
 struct Student
 {
     //成员具体信息
     string name;
     int age;
     int score;
 }s1;
 //创建时顺便创建结构体变量
 //通过学生类型创建具体学生
 int main()
 {
 
     s1.name = "王五";
     s1.age = 20;
     s1.score = 120;
     cout << "姓名:" << s1.name << endl;
     cout << "年龄:" << s1.age << endl;
     cout << "分数:" << s1.score << endl;
     return 0;
 };

用.来访问结构体变量中的值

8.3 结构体数组

将自定义的结构体放入数组中方便维护




 #include<iostream>
 using namespace std;
 struct student
 {
     string name;
     int age;
 };
 int main()
 {
     struct student arr[3] =
     {
         {"张三",18},
         {"李四",19},
         {"王五",20}
     };
     //给其中的元素赋值
     arr[1].age = 20;
     arr[1].name = "赵六";
     //输出结果
     for (int i = 0; i < 3; i++)
     {
         cout << "姓名:" << arr[i].name << " 年龄: " << arr[i].age << endl;
     }
     return 0;
 }



8.4 结构体指针



 #include<iostream>
 using namespace std;
 #include<string>
 struct student
 {
     string name;
     int age;
     int score;
 };
 
 int main()
 {
     struct student s = { "张三",18,100 };
     struct student* p = &s;//通过指针指向结构体变量
     cout << " 姓名 " << p->name 
          << " 年龄 " << p->age 
          << " 分数 " << p->score << endl;
     cout << " 姓名 " << s.name
          << " 年龄 " << s.age
          << " 分数 " << s.score << endl;
     //上面的两个输出语句等价
     return 0;
 }

8.5 结构体嵌套结构体



 #include<iostream>
 using namespace std;
 #include<string>
 struct student
 {
     string name;
     int age;
     int score;
 };  
 struct teacher
 {
     int id;
     string name;
     int age;
     struct student std;//student结构体要先定义
 };
 int main()
 {
     struct teacher t;
     t.id = 1000;
     t.name = "老王";
     t.age = 50;
     t.std.name = "小王";//通过teacher结构体继续控制student结构体
     t.std.age = 18;
     t.std.score = 100;
     cout << "老师姓名:" << t.name << endl
          << "老师编号: " << t.id << endl
          << "老师年龄: " << t.age << endl
          << "老师辅导学生的姓名: " << t.std.name << endl
          << "老师辅导学生的年龄: " << t.std.age << endl
          << "老师辅导学生的分数: " << t.std.score << endl;
     return 0;
 }

也可以在变量t的中括号内依次输入

 #include<iostream>
 using namespace std;
 #include<string>
 struct student
 {
     string name;
     int age;
     int score;
 };  
 struct teacher
 {
     int id;
     string name;
     int age;
     struct student std;//student结构体要先定义
 };
 int main()
 {
     struct teacher t {10,"老李",50,"小李",19,110 };
     //t.id = 1000;
     //t.name = "老王";
     //t.age = 50;
     //t.std.name = "小王";//通过teacher结构体继续控制student结构体
     //t.std.age = 18;
     //t.std.score = 100;
     cout << "老师姓名:" << t.name << endl
          << "老师编号: " << t.id << endl
          << "老师年龄: " << t.age << endl
          << "老师辅导学生的姓名: " << t.std.name << endl
          << "老师辅导学生的年龄: " << t.std.age << endl
          << "老师辅导学生的分数: " << t.std.score << endl;
     return 0;
 }

8.6 结构体做函数参数



值传递

 #include<iostream>
 using namespace std;
 #include<string>
 struct student
 {
     string name;
     int age;
     int score;
 };  
 void p(struct student s)
 {
     cout << "姓名:" << s.name 
         << " 年龄:" << s.age 
         << " 分数:" << s.score << endl;
 }
 int main()
 {
     struct student s = { "张三",18,100 };
     p(s);
     return 0;
 }

地址传递

 #include<iostream>
 using namespace std;
 #include<string>
 struct student
 {
     string name;
     int age;
     int score;
 };  
 void p(struct student* s)
 {
     s->age = 200;
     cout << "姓名:" << s->name 
         << " 年龄:" <<  s->age
         << " 分数:" << s->score << endl;//调用指针用箭头
 }
 int main()
 {
     struct student s = { "张三",18,100 };
     p(&s);
     cout << "main函数中张三的年龄:" << s.age << endl;
     return 0;
 }

地址传递的情况下形参改变会影响实际参数,而值传递不会

8.7 结构体中const的使用场景

目的:用const防止误操作

 #include<iostream>
 using namespace std;
 #include<string>
 struct student
 {
     string name;
     int age;
     int score;
 };  
 void p(const struct  student *s)//在最前面加上const
 {
     s->age = 1;//报错,防止出现误操作
     cout << "姓名:" << s->name
         << " 年龄:" << s->age
         << " 分数:" << s->score << endl;
 }
 int main()
 {
     struct student s = { "张三",18,100 };
     p(&s);//用指针访问可以减少内存的使用
     
     return 0;
 }



相关推荐

Python操作Word文档神器:python-docx库从入门到精通

Python操作Word文档神器:python-docx库从入门到精通动动小手,点击关注...

Python 函数调用从入门到精通:超详细定义解析与实战指南 附案例

一、函数基础:定义与调用的核心逻辑定义:函数是将重复或相关的代码块封装成可复用的单元,通过函数名和参数实现特定功能。它是Python模块化编程的基础,能提高代码复用性和可读性。定义语法:...

等这么长时间Python背记手册终于来了,入门到精通(视频400集)

本文毫无套路!真诚分享!前言:无论是学习任何一门语言,基础知识一定要扎实,基础功非常的重要,找一个有丰富编程经验的老师或者师兄带着你会少走很多弯路,你的进步速度也会快很多,无论我们学习的目的是什么,...

图解Python编程:从入门到精通系列教程(附全套速查表)

引言本系列教程展开讲解Python编程语言,Python是一门开源免费、通用型的脚本编程语言,它上手简单,功能强大,它也是互联网最热门的编程语言之一。Python生态丰富,库(模块)极其丰富,这使...

Python入门教程(非常详细)从零基础入门到精通,看完这一篇就够

本书是Python经典实例解析,采用基于实例的方法编写,每个实例都会解决具体的问题和难题。主要内容有:数字、字符串和元组,语句与语法,函数定义,列表、集、字典,用户输入和输出等内置数据结构,类和对象,...

Python函数全解析:从入门到精通,一文搞定!

1.为什么要用函数?函数的作用:封装代码,提高复用性,减少重复,提高可读性。...

Python中的单例模式:从入门到精通

Python中的单例模式:从入门到精通引言单例模式是一种常用的软件设计模式,它保证了一个类只有一个实例,并提供一个全局访问点。这种模式通常用于那些需要频繁创建和销毁的对象,比如日志对象、线程池、缓存等...

【Python王者归来】手把手教你,Python从入门到精通!

用800个程序实例、5万行代码手把手教你,Python从入门到精通!...

Python从零基础入门到精通:一个月就够了

如果想从零基础到入门,能够全职学习(自学),那么一个月足够了。...

Python 从入门到精通:一个月就够了

要知道,一个月是一段很长的时间。如果每天坚持用6-7小时来做一件事,你会有意想不到的收获。作为初学者,第一个月的月目标应该是这样的:熟悉基本概念(变量,条件,列表,循环,函数)练习超过30个编...

Python零基础到精通,这8个入门技巧让你少走弯路,7天速通编程!

Python学习就像玩积木,从最基础的块开始,一步步搭建出复杂的作品。我记得刚开始学Python时也是一头雾水,走了不少弯路。现在回头看,其实掌握几个核心概念,就能快速入门这门编程语言。来聊聊怎么用最...

神仙级python入门教程(非常详细),从0到精通,从看这篇开始!

python入门虽然简单,很多新手依然卡在基础安装阶段,大部分教程对一些基础内容都是一带而过,好多新手朋友,对一些基础知识常常一知半解,需要在网上查询很久。...

Python类从入门到精通,一篇就够!

一、Python类是什么?大家在生活中应该都见过汽车吧,每一辆真实存在、能在路上跑的汽车,都可以看作是一个“对象”。那这些汽车是怎么生产出来的呢?其实,在生产之前,汽车公司都会先设计一个详细的蓝图...

学习Python从入门到精通:30天足够了,这才是python基础的天花板

当年2w买的全套python教程用不着了,现在送给有缘人,不要钱,一个月教你从入门到精通1、本套视频共487集,本套视频共分4季...

30天Python 入门到精通(3天学会python)

以下是一个为期30天的Python入门到精通学习课程,专为零基础新手设计。课程从基础语法开始,逐步深入到面向对象编程、数据处理,最后实现运行简单的大语言模型(如基于HuggingFace...

取消回复欢迎 发表评论: