网站首页 > 基础教程 正文
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;
}
猜你喜欢
- 2024-12-30 C++ 在编程语言谱系中的位置及其特点
- 2024-12-30 C++基础知识总结(超详细总结) c++ 基础
- 2024-12-30 [面试]C++中struct和class的区别是什么
- 2024-12-30 C++类和C语言的结构体有什么区别?linux C++第18讲
- 2024-12-30 C与C++利用pragma pack对结构体做成员打包对齐设置
- 2024-12-30 结构体所占内存大小 结构体所占内存大小是所有成员所占的内存字节数计算
- 2024-12-30 结构体的各种使用方法详细讲解-c\c++
- 2024-12-30 C++,结构体和类,结构体和类的区别,struct与class的区别
- 2024-12-30 C++编程:复合数据类型—结构体 c语言结构体复数运算
- 2024-12-30 C#与C++交互开发系列之复杂类型传递与解析
- 05-24php实现三方支付的方法有哪些?
- 05-24CosmicSting 漏洞影响 75% 的 Adobe Commerce 和 Magento 网站
- 05-24Java接口默认方法的奇妙用途
- 05-24抽象类和接口
- 05-24详解Java抽象类和接口
- 05-24拒绝接口裸奔!开放API接口签名验证
- 05-24每天学Java!Java中的接口有什么作用
- 05-24Java:在Java中使用私有接口方法
- 最近发表
- 标签列表
-
- jsp (69)
- gitpush (78)
- gitreset (66)
- python字典 (67)
- dockercp (63)
- gitclone命令 (63)
- dockersave (62)
- linux命令大全 (65)
- pythonif (86)
- location.href (69)
- dockerexec (65)
- tail-f (79)
- deletesql (62)
- c++模板 (62)
- linuxgzip (68)
- 字符串连接 (73)
- nginx配置文件详解 (61)
- html标签 (69)
- c++初始化列表 (64)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- console.table (62)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)