C++笔记:struct结构体 c++结构体使用
yuyutoo 2024-10-12 00:45 9 浏览 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;
}
相关推荐
- ETCD 故障恢复(etc常见故障)
-
概述Kubernetes集群外部ETCD节点故障,导致kube-apiserver无法启动。...
- 在Ubuntu 16.04 LTS服务器上安装FreeRADIUS和Daloradius的方法
-
FreeRADIUS为AAARadiusLinux下开源解决方案,DaloRadius为图形化web管理工具。...
- 如何排查服务器被黑客入侵的迹象(黑客 抓取服务器数据)
-
---排查服务器是否被黑客入侵需要系统性地检查多个关键点,以下是一份详细的排查指南,包含具体命令、工具和应对策略:---###**一、快速初步检查**####1.**检查异常登录记录**...
- 使用 Fail Ban 日志分析 SSH 攻击行为
-
通过分析`fail2ban`日志可以识别和应对SSH暴力破解等攻击行为。以下是详细的操作流程和关键分析方法:---###**一、Fail2ban日志位置**Fail2ban的日志路径因系统配置...
- 《5 个实用技巧,提升你的服务器安全性,避免被黑客盯上!》
-
服务器的安全性至关重要,特别是在如今网络攻击频繁的情况下。如果你的服务器存在漏洞,黑客可能会利用这些漏洞进行攻击,甚至窃取数据。今天我们就来聊聊5个实用技巧,帮助你提升服务器的安全性,让你的系统更...
- 聊聊Spring AI Alibaba的YuQueDocumentReader
-
序本文主要研究一下SpringAIAlibaba的YuQueDocumentReaderYuQueDocumentReader...
- Mac Docker环境,利用Canal实现MySQL同步ES
-
Canal的使用使用docker环境安装mysql、canal、elasticsearch,基于binlog利用canal实现mysql的数据同步到elasticsearch中,并在springboo...
- RustDesk:开源远程控制工具的技术架构与全场景部署实战
-
一、开源远程控制领域的革新者1.1行业痛点与解决方案...
- 长安汽车一代CS75Plus2020款安装高德地图7.5
-
不用破解原车机,一代CS75Plus2020款,安装车机版高德地图7.5,有红绿灯读秒!废话不多讲,安装步骤如下:一、在拨号状态输入:在电话拨号界面,输入:*#518200#*(进入安卓设置界面,...
- Zookeeper使用详解之常见操作篇(zookeeper ui)
-
一、Zookeeper的数据结构对于ZooKeeper而言,其存储结构类似于文件系统,也是一个树形目录服务,并通过Key-Value键值对的形式进行数据存储。其中,Key由斜线间隔的路径元素构成。对...
- zk源码—4.会话的实现原理一(会话层的基本功能是什么)
-
大纲1.创建会话...
- Zookeeper 可观测性最佳实践(zookeeper能够确保)
-
Zookeeper介绍ZooKeeper是一个开源的分布式协调服务,用于管理和协调分布式系统中的节点。它提供了一种高效、可靠的方式来解决分布式系统中的常见问题,如数据同步、配置管理、命名服务和集群...
- 服务器密码错误被锁定怎么解决(服务器密码错几次锁)
-
#服务器密码错误被锁定解决方案当服务器因多次密码错误导致账户被锁定时,可以按照以下步骤进行排查和解决:##一、确认锁定状态###1.检查账户锁定状态(Linux)```bash#查看账户锁定...
- zk基础—4.zk实现分布式功能(分布式zk的使用)
-
大纲1.zk实现数据发布订阅...
- 《死神魂魄觉醒》卡死问题终极解决方案:从原理到实战的深度解析
-
在《死神魂魄觉醒》的斩魄刀交锋中,游戏卡死犹如突现的虚圈屏障,阻断玩家与尸魂界的连接。本文将从技术架构、解决方案、预防策略三个维度,深度剖析卡死问题的成因与应对之策,助力玩家突破次元壁障,畅享灵魂共鸣...
你 发表评论:
欢迎- 一周热门
-
-
前端面试:iframe 的优缺点? iframe有那些缺点
-
带斜线的表头制作好了,如何填充内容?这几种方法你更喜欢哪个?
-
漫学笔记之PHP.ini常用的配置信息
-
推荐7个模板代码和其他游戏源码下载的网址
-
其实模版网站在开发工作中很重要,推荐几个参考站给大家
-
[干货] JAVA - JVM - 2 内存两分 [干货]+java+-+jvm+-+2+内存两分吗
-
正在学习使用python搭建自动化测试框架?这个系统包你可能会用到
-
织梦(Dedecms)建站教程 织梦建站详细步骤
-
【开源分享】2024PHP在线客服系统源码(搭建教程+终身使用)
-
2024PHP在线客服系统源码+完全开源 带详细搭建教程
-
- 最近发表
-
- ETCD 故障恢复(etc常见故障)
- 在Ubuntu 16.04 LTS服务器上安装FreeRADIUS和Daloradius的方法
- 如何排查服务器被黑客入侵的迹象(黑客 抓取服务器数据)
- 使用 Fail Ban 日志分析 SSH 攻击行为
- 《5 个实用技巧,提升你的服务器安全性,避免被黑客盯上!》
- 聊聊Spring AI Alibaba的YuQueDocumentReader
- Mac Docker环境,利用Canal实现MySQL同步ES
- RustDesk:开源远程控制工具的技术架构与全场景部署实战
- 长安汽车一代CS75Plus2020款安装高德地图7.5
- Zookeeper使用详解之常见操作篇(zookeeper ui)
- 标签列表
-
- mybatis plus (70)
- scheduledtask (71)
- css滚动条 (60)
- java学生成绩管理系统 (59)
- 结构体数组 (69)
- databasemetadata (64)
- javastatic (68)
- jsp实用教程 (53)
- fontawesome (57)
- widget开发 (57)
- vb net教程 (62)
- hibernate 教程 (63)
- case语句 (57)
- svn连接 (74)
- directoryindex (69)
- session timeout (58)
- textbox换行 (67)
- extension_dir (64)
- linearlayout (58)
- vba高级教程 (75)
- iframe用法 (58)
- sqlparameter (59)
- trim函数 (59)
- flex布局 (63)
- contextloaderlistener (56)