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

C++之结构体

结构体

//一、结构体的概念、定义和使用


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

#include<iostream>
using namespace std;
#include<string>
//1.创建学生数据类型:学生包括(姓名,年龄,分数)
//自定义数据类型,一些类型集合组成的一个类型
//语法 strcut 类型名称{成员列表};  注意后面有;
struct Student
{//成员列表//姓名string name;//年龄int age;//分数int score;
}s3;//2.通过学生类型创建具体学生int main()
{
//2.1 struct Student s1//strcut关键字可以省略//struct Student s1;Student s1;//给s1属性赋值,通过.访问结构体变量中的属性s1.name = "张三";s1.age = 18;s1.score = 100;cout << "姓名:" << s1.name << " 年龄:" << s1.age << " 分数:" << s1.score << endl;
//2.2 struct Student s2={...}struct Student s2 = { "李四",19,80 };cout << "姓名:" << s2.name << " 年龄:" << s2.age << " 分数:" << s2.score << endl;//2.3 在定义结构体时顺便创建结构体变量(如果没有s3,则上面数据{}后要加;s3.name = "王五";s3.age = 20;s3.score = 60;cout << "姓名:" << s3.name << " 年龄:" << s3.age << " 分数:" << s3.score << endl;//一般用第1或2种return 0;
}

//二、结构体数组


//作用:将自定义的结构体放入到数组中方便维护
//语法:struct 结构体名 数组名{元素个数}={ {},{},...{} }

#include<iostream>
using namespace std;//结构体数组
//1.定义结构体
struct Student
{string name;int age;int score;
};
int main()
{//2.创建结构体数组struct Student stuArray[3] ={{"张三",18,100},{"李四",28,99},{"王五",38,66}};//3.给结构体数组中的元素赋值//一开始不知道人家名字,后面改stuArray[2].name = "赵六";stuArray[2].age = 80;stuArray[2].score = 80;//4.便利结构体数组for (int i = 0; i < 3; i++){cout    << " 姓名: " << stuArray[i].name << " 年龄: " << stuArray[i].age << " 分数: " << stuArray[i].score << endl;}return 0;
}

//三、结构体指针


//作用:通过指针访问结构体中的成员 利用操作符->

#include<iostream>
using namespace std;struct student
{string name;int age;int scroe;
};
int main()
{//1.创建学生结构体变量struct student s = { "张三",18,100 };//struct可省略//2.通过指针指向结构体变量struct student* p = &s;//struct可省略//3.通过指针访问结构体变量中的数据//通过结构体指针 访问结构体中的属性,需要利用'->'cout << "姓名: " << p->name << " 年龄: " << p->age << " 分数: " << p->scroe << endl;return 0;
}

//四、结构体嵌套结构体


//作用:结构体中的成员可以是另一个结构体

#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 s;
};int main()
{teacher t;t.id = 18888;t.name = "老王";t.age = 50;t.s.name = "小王";t.s.age = 20;t.s.score = 60;cout << "老师姓名: " << t.name << " 老师编号: " << t.id << " 老师年龄: " << t.age<< " 老师辅导学生姓名:  " << t.s.name << " 学生年龄: " << t.s.age << "  学生分数: " << t.s.score << endl;return 0;
}

//五、结构体做函数参数


//作用:将结构体作为参数向函数中传递
//传递方式有两种:值传递和地址传递

#include<iostream>
using namespace std;
#include<string>struct student
{string name;int age;int score;
};
//打印学生信息函数
//1.值传递
void printStudent1(struct student s)
{s.age = 100;cout << " 子函数1中 姓名:" << s.name << " 年龄:" << s.age << " 分数: " << s.score << endl;
}
//2.地址传递
void printStudent2(struct student * p)
{p->age = 200;cout << "子函数2中 姓名:" << p->name << " 年龄: " << p->age << " 分数: " << p->score << endl;
}
int main()
{//结构题做函数参数//将学生传入到一个参数中,打印学生身上的所有信息//创建一个结构体变量struct student s;s.name = "张三";s.age = 18;s.score = 85;printStudent1(s);printStudent2(&s);cout << "main函数中打印 姓名:" << s.name << " 年龄: " << s.age << " 分数:" << s.score<<endl;return 0;
}

//六、结构体中const使用场景


//作用:用const来防止误操作

#include<iostream>
using namespace std;
#include<string>//const的使用场景struct student
{string name;int age;int score;
};//将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本出来
void printStudent(const student *s)//指针目的是节省空间,const是防止修改了形参把实参变了,即防止误操作
{s->age = 150;//(误操作了)//但是利用了const age还是为15岁  只能读不能写cout << "姓名: " << s->name << " 年龄: " << s->age << " 得分: " << s->score << endl;
}int main()
{//创建结构体变量struct student s = { "张三",15,70 };//通过函数打印结构变量信息printStudent(&s);cout << s.age;return 0;
}

http://www.lryc.cn/news/309320.html

相关文章:

  • 分布式ID选型对比(1)
  • T-SQL 高阶语法之存储过程
  • 解决鸿蒙模拟器卡顿的问题
  • 【LeetCode每日一题】【BFS模版与例题】863.二叉树中所有距离为 K 的结点
  • 设计模式-结构模式-装饰模式
  • MySQL:一行记录如何
  • ‘grafana.ini‘ is read only ‘defaults.ini‘ is read only
  • 博途PLC 面向对象系列之“输送带控制功能块“(SCL代码)
  • 2024-02学习笔记
  • 最新消息:英特尔宣布成立全新独立运营的FPGA公司——Altera
  • RC正弦波振荡电路
  • 【Git学习笔记】提交PR
  • 线程池的相关参数
  • 图书推荐||Word文稿之美
  • 前端导出word文件的多种方式、前端导出excel文件
  • Linux和Windows操作系统在腾讯云幻兽帕鲁服务器上的内存占用情况如何?
  • 腾讯云4核8G的云服务器性能水平?使用场景说明
  • 1_SQL
  • PoC免写攻略
  • c1-周考2
  • express+mysql+vue,从零搭建一个商城管理系统7--文件上传,大文件分片上传
  • markdown的使用(Typora)
  • 【python】json转成成yaml中文编码异常显示成:\u5317\u4EAC\u8DEF123\u53F7
  • Python 实现Excel自动化办公(中)
  • MCTS代码
  • Java 中notify 和 notifyAll 方法介绍
  • Leetcode :杨辉三角
  • MWC 2024丨美格智能CEO杜国彬出席中国联通创新成果发布会并发表主题演讲
  • 个人建站前端篇(七)vite + vue3企业级项目模板
  • centos7 安装 docker-compose