结构体基本概念:

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

结构体定义和使用:

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

   通过结构体创建变量名的方式有三种:

          1、struct 结构体名 变量名

          2、struct 结构体名 变量名 = { 成员1值,成员2值...}

          3、定义结构体时顺便创建变量

   实例:

#include<iostream>
#include<string>
using namespace std; //1、创建学生数据类型 : 学生包括(姓名、年龄、分数) struct Student
{
//成员列表
string name; //姓名
int age; //年龄
int score; //分数
}s3; //顺便创建结构体变量


//2、通过学生类型创建具体学生

int main()
{ //2.1 struct Student s1
//struct 关键字 在C++中可以省略 相当于下行
Student s1;//struct 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.name = "王五";
s3.age = 30;
s3.score = 60;
cout << "姓名:" << s3.name << " " << "年龄:" << s3.age << " " << "分数:" << s3.score << endl; system("pause");
return 0;
}

结构体数组:

作用:将自定义的结构体放入到数组中方便维护;

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

实例:

#include<iostream>
#include<string>
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 = 60;
//4、遍历结构体数组
for (int i = 0; i < 3; i++)
{
cout << "姓名:" << stuArray[i].name << " "
<< "年龄:" << stuArray[i].age <<" "
<< "分数:" << stuArray[i].score << endl;
}
system("pause");
return 0;
}

结构体指针

作用:通过指针访问结构体中的成员;利用操作符 -> 可以通过结构体指针访问结构体属性。

实例:

#include<iostream>
#include<string>
using namespace std; //结构体指针
//1、定义学生结构体 struct Student
{
string name; //姓名
int age; //年龄
int score; //分数
}; int main()
{
//2、创建学生结构体变量
//struct Student s = {"张三",18,100}; //struct可以省略
Student s = {"张三",18,100}; //3、通过指针指向结构体变量
//int * p = &s; ->!!! 错误!!! <- 因为上行代码指定s是Student类型的,所以不能返回int型的数据,而应该返回Student型,即不能用int型的指针指向s; //struct Student * p = &s; //struct可以省略
Student * p = &s; //4、通过指针访问结构体中的数据
cout << "姓名:" << p->name <<" " << "年龄:" << p->age <<" " << "分数" << 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; //年龄
struct Student stu; //所带的学生
}; int main()
{
Teacher t;
t.id = 10000;
t.name = "老王";
t.age = 50;
t.stu.name = "小王";
t.stu.age = 20;
t.stu.score = 60; cout << "老师姓名:" << t.name <<" " << "老师编号:" << t.id << " " << "老师年龄:" << t.age << " "
<< "老师辅导的学生姓名:" << t.stu.name << " " << "学生分数:" << t.stu.score << " " << "学生年龄:" << t.stu.age << endl; system("pause");
return 0;
}

结构体做函数参数

作用:将结构体作为参数向函数中传递

传递的方式有两种:1、值传递;2、地址传递;

实例:

#include<iostream>
#include<string>
using namespace std; //结构体做函数参数 //定义学生结构体
struct Student
{
string name; //姓名
int age; //年龄
int score; //分数
}; //1、值传递
void printStudent1(struct Student s)
{
cout << "子函数中通过值传递打印 姓名:" << s.name <<" " << "年龄:" << s.age <<" " << "分数:" << s.score << endl;
}
//2、地址传递
void printStudent2(struct Student * p)
{
cout << "子函数中通过地址传递打印 姓名:" << p->name << " " << "年龄:" << p->age << " " << "分数:" << p->score << endl;
}
int main()
{
//将学生传入到一个参数中,打印学生身上的所有信息
struct Student s;
s.name = "小王";
s.age = 20;
s.score = 99; //cout << "main函数中打印 姓名:" << s.name <<" " << "年龄:" << s.age <<" " << "分数:" << s.score << endl;
//printStudent1(s);
printStudent2(&s);
system("pause");
return 0;
}

值传递和地址传递区别:

值传递:

void printStudent1(struct Student s)
{
s.age = 100;
cout << "子函数中通过值传递打印 姓名:" << s.name <<" " << "年龄:" << s.age <<" " << "分数:" << s.score << endl;
}
int main()
{
struct Student s;
s.name = "小王";
s.age = 20;
s.score = 99; printStudent1(s);
//printStudent2(&s);
cout << "main函数中打印 姓名:" << s.name <<" " << "年龄:" << s.age <<" " << "分数:" << s.score << endl; system("pause");
return 0;
}

打印结果如下:

值传递中,形参()发生任何的改变,都不会修饰实参(),即形参的值发生改变,实参的值不会随着形参的值发生改变。

地址传递: 

void printStudent2(struct Student * p)
{
p->age = 100;
cout << "子函数中通过地址传递打印 姓名:" << p->name << " " << "年龄:" << p->age << " " << "分数:" << p->score << endl;
}
int main()
{
//将学生传入到一个参数中,打印学生身上的所有信息
struct Student s;
s.name = "小王";
s.age = 20;
s.score = 99; //printStudent1(s);
printStudent2(&s);
cout << "main函数中打印 姓名:" << s.name <<" " << "年龄:" << s.age <<" " << "分数:" << s.score << endl; system("pause");
return 0;
}

打印结果为

地址传递的形参()会修饰实参();即形参值发生改变,实参也会随着改变。

结构体中const使用场景

作用:用const来防止误操作

实例:

#include<iostream>
#include<string>
using namespace std; //const使用场景 struct Student
{
string name; //姓名
int age; //年龄
int score; //分数
}; //void printStudents(Student s) //值传递会把形参的所有值(本例中的姓名、年龄、分数)复制一份,再传给实参,这样导致内存开销太大
//void printStudents(Student *s) //将函数中的形参改为指针,可以减少内存空间的使用,减小内存开销,而不会像值传递一样复制一个新的副本出来 //加入const 就不能修改参数值了,只能读取 避免以后因为代码量太大而不小心误操作改值
void printStudents(const Student* s) //加入const 就不能修改了,只能读取 避免以后因为代码量太大而不小心误操作改值
{
/*
//s.age = 150;
//cout << "姓名:" << s.name << " " << "年龄:" << s.age << " " << "分数:" << s.score << endl;
*/ // s->age = 150; //加入const 就不能修改参数了,否则运行会报错:表达式必须是可修改的左值!以此用来限定用户误操作
cout << "姓名:" << s->name << " " << "年龄:" << s->age << " " << "分数:" << s->score << endl;
}
int main()
{
//创建结构体变量
struct Student s = { "张三",20,90 }; //通过函数打印结构体变量信息
//printStudents(s); //值传递内存开销太大
printStudents(&s); //采用地址传递,每次只占用4个字节(指针内存大小为4字节),减小内存开销 cout << "张三的年龄为:" << s.age << endl; system("pause");
return 0;
}

C++学习 3 结构体的更多相关文章

  1. C语言学习之结构体

    前言 一直以来,C语言的学习都在入门阶段,只用到数组.函数.循环.选择.位运算这些基本的知识,较少用到指针.预处理.结构体.枚举类型.文件操作等这些C语言的精髓内容,现在想想真不敢说自己熟练掌握C语言 ...

  2. c语言学习笔记 - 结构体位域

    在学习结构体的时候遇到了位域这个概念,位域主要是为了节省内存空间,比如用一个32位,4个字节的int存储一个开关变量时,会造成空间浪费,于是干脆就考虑在这个32划分不同的区域来存储数据,例如划出1位存 ...

  3. C语言学习1——结构体剖析

    一、定义结构体变量的方法 1.1先声明结构体类型在定义变量名 例如: a.声明结构体类型 struct student { int num; char name[20]; char sex; int ...

  4. C语言学习笔记--结构体

    结构体定义三种方式: #include<stdio.h> //第一种定义方法 struct point { int x; int y; }; struct point p1,p2; //第 ...

  5. C学习之结构体

    结构体(struct) 结构体是由基本数据类型构成的.并用一个标识符来命名的各种变量的组合,结构体中可以使用不同的数据类型. 1. 结构体说明和结构体变量定义 在Turbo C中, 结构体也是一种数据 ...

  6. SNMP学习之结构体snmp_secmod_def

    此结构体中定义了各个回调函数,在函数init_ksm(E:\code\net-snmp-5.4.2.1\snmplib)中进行了初始化. void init_ksm(void) { struct sn ...

  7. 【CUDA学习】结构体指针复制

    内核函数中要用data结构作用参数 typedef struct { int* value; int* num; } data; //host端 data* h_input; h_input=(dat ...

  8. go学习笔记-结构体

    结构体 结构体是由一系列具有相同类型或不同类型的数据构成的数据集合 定义 格式 type struct_variable_type struct { member definition; member ...

  9. iOS开发-Object-C学习之结构体使用

    前言:定义结构体并不是定义一个变量,而是定义了种数据类型. 结构体作用: 结构体和其他类型基础数据类型一样,例如int类型,char类型 只不过结构体可以做成你想要的数据类型.以方便日后的使用. 在实 ...

  10. libevent源码学习_event结构体

    在libevent中最重要的结构体莫过于event和event_base了,下面对于这2个结构体进行分析. 1.结构体event,位于:event.h struct event { /* * 双向链表 ...

随机推荐

  1. struts2思想学习(一)

    OOP 面向对象编程 AOP 面向切面编程 而在struts2 处处体现了面向切面编程的思想(动态代理最典型)! 拦截器其实也是面向切面编程!拦截器切断了所有请求到action的操作 并做了很多的前提 ...

  2. MySql 改变table的列名以及列的类型

    1 ALTER TABLE table_name CHANGE COLUMN old_col_name new_col_name datatype; #改变table的列名以及列的类型

  3. 有关Java动态数组的一个小问题

    前言 问题描述 今天遇到一个关于集合的问题,觉得比较有趣,记录一下,这个问题是:定义一个用户类,至少包含姓名,年龄,生日,qq邮箱,初始化10个用户,利用String操作,提取qq到List集合中,姓 ...

  4. 整理之BroadcaseReceiver

    广播的分类 有序广播:按接收器优先级从高到低接受消息,一次只能有一个接收器处理消息.中途可以被截断. 无序广播:所有接收器同时接受消息并处理,无法拦截. 本地广播:只能在本应用内传播的无需广播.上面两 ...

  5. 新版idea无法导入mavenweb模板

    目前没有任何办法,最好是下个旧版的

  6. (一)Superset 1.3图表篇——Table

    本系列文章基于Superset 1.3.0版本.1.3.0版本目前支持分布,趋势,地理等等类型共59张图表.本次1.3版本的更新图表有了一些新的变化,而之前也一直没有做过非常细致的图表教程. 而且目前 ...

  7. AWS扩容EC2实例根空间

    文章原文 aws 端操作 先在EC2 实例中选中磁盘 然后打开跟设备 修改大小后保存 ec2 端操作 lsblk 查看当前设备的磁盘编号 df -T -H 查看扩容前的空间大小并确定磁盘格式 grow ...

  8. python爬区csdn文章信息(原始稿)

    使用python对csdn的博主文章进行爬取,期间又遇到了新的问题和旧的已经遇到过的问题.首先做一个笔记,免得以后遇到同样的问题时还得浪费时间和经历. 刚开始目的没那么明确,主要在于熟悉相关的规则及流 ...

  9. Excel导入保存附件和解析数据

    Excel导入保存附件和解析数据 一,前端上传附件的组件 1.先给一个下载模板的按钮 // 下载Excel模板 downLoadExcel: function () { window.open(GLO ...

  10. HCNP Routing&Switching之IS-IS邻居建立、LSDB同步、拓扑计算和路由形成

    前文我们了解了IS-IS的报文结构和类型相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/15260670.html:今天我们来聊一聊IS-IS建立邻居. ...