03—C++结构体

01结构体的定义和使用

#include<iostream>;
using namespace std;
//使用C++中的字符串要包含这个头文件
#include<string>;
//结构体的基本概念:结构体属于用户自定义的数据类型,允许存储不同类型的数据
/*
先定义结构体,然后通过结构体创建变量

语法:struct 结构体命名{结构体成员变量};
通过结构体创建变量的方式有三种
1.struct 结构体名 变量名
2.struct 结构体名 变量名={成员1值,成员2值,,,}
3.定义结构体时顺便创建变量
*/

//1.创建一个学生的数据类型:学生包括(姓名,年龄,分数)
struct Student
{
	//成员变量
	string name;
	int age;
	int score;
};
//2.通过学生类型创建具体的学生
//注意:在结构体变量创建的时候关键字可以省略,定义的时候不可以省略
int main(){
	//2.1.struct 结构体名 变量名
	struct Student s1;
	//通过点来访问结构体变量中的属性
	s1.name = "AISMALL_01";
	s1.age = 18;
	s1.score = 150;
	cout << "姓名:" << s1.name << "年龄:" << s1.age << "分数:" << s1.score << endl;
	//2.2.struct 结构体名 变量名 = { 成员1值, 成员2值,,, }
	struct Student s2 = { "AISMALL_02", 19, 150 };
	cout << "姓名:" << s2.name << "年龄:" << s2.age << "分数:" << s2.score << endl;
	//2.3.定义结构体时顺便创建变量
	struct Student1
	{
		//成员变量
		string name;
		int age;
		int score;
	}s3;
	s3.name = "AISMALL_03";
	s3.age = 20;
	s3.score = 150;
	cout << "姓名:" << s3.name << "年龄:" << s3.age << "分数:" << s3.score << endl;
	system("pause");
	return 0;
}

02结构体数组

#include<iostream>;
using namespace std;
//使用C++中的字符串要包含这个头文件
#include<string>;
//结构体数组
//1.定义结构体
struct Student
{
	//成员变量
	string name;
	int age;
	int score;
};
int main(){
	//2.创建结构体数组
	struct Student stuArray[3]=
	{
		{ "AISMALL_01", 18, 150 },
		{ "AISMALL_02", 19, 150 },
		{ "AISMALL_03", 20, 150 } 
	};
	//3.给结构体中的元素赋值
	stuArray[1].age = 23;
	//4.遍历结构体中的数组
	for (int i = 0; i < 3; i++){
		stuArray[i];
		cout << "姓名:" << stuArray[i].name << "年龄:" << stuArray[i].age << "分数:" << stuArray[i].score << endl;
	}
	system("pause");
	return 0;
}

03结构体指针

#include<iostream>;
using namespace std;
//使用C++中的字符串要包含这个头文件
#include<string>;
//通过指针访问结构体中的成员
//利用操作符->可以通过,结构体指针,访问结构体属性

//定义结构体
struct Student
{
	//成员变量
	string name;
	int age;
	int score;
};
int main(){
	//1.创建结构变量
		struct Student s1 = { "AISMALL_01", 18, 150 };
		struct Student s2 = { "AISMALL_02", 19, 150 };
		struct Student s3 = { "AISMALL_03", 20, 150 };
	//2.通过指针指向结构体变量(struct可以省略)
		struct Student *p;
		p = &s1;
	//3.通过指针访问结构体变量中的数据
		cout << "姓名:" << p->name << "年龄:" << p->age << "分数:" << p->score << endl;
	system("pause");
	return 0;
}

04结构体嵌套结构体

#include<iostream>;
using namespace std;
//使用C++中的字符串要包含这个头文件
#include<string>;
//结构体嵌套:例如一个老师带多个学生
//利用操作符->可以通过,结构体指针,访问结构体属性

//定义学生结构体
struct Student
{
	//成员变量
	string name;
	int age;
	int score;
};
//定义老师结构体,并在老师结构体中嵌套学生结构体
struct Teacher
{
	int id;
	string name;
	int age;
	struct Student stu;
};
int main(){
	struct Teacher t1 = { 011, "dongdong", 38, { "AISMALL_01", 18, 150 } };
	cout << "老师姓名:" << t1.name << "学生姓名:" << t1.stu.name << endl;
	system("pause");
	return 0;
}

05结构体做函数参数

#include<iostream>;
using namespace std;
//使用C++中的字符串要包含这个头文件
#include<string>;
//结构体做函数参数向函数中传递
//传递方式有两种:第一种,值传递,       第二种,地址传递

//将学生传入到一个参数中,打印学生身上的所有信息

//定义学生结构体
struct Student
{
	//成员变量
	string name;
	int age;
	int score;
};
//打印学生信息的函数
//注意:结构体是一种数据类型,定义结构体类型的变量(类比:基本数据类型中的整形int)
void printStudent1(struct Student s){
	s.age = 20;
	cout << "子函数中 姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl;
}

void printStudent2(struct Student *s){
	//指针访问成员变量需要用箭头
	s->age = 22;
	cout << "子函数中 姓名:" << s->name << " 年龄:" << s->age << " 分数:" << s->score << endl;
}
int main(){
	//创建学生结构体变量
	struct Student s1 ={ "AISMALL_01", 18, 150 };
	cout <<"未修改年龄的值:" << s1.age << endl;
	
	//值传递
	printStudent1(s1);
	cout << "值传递的子函数修改年龄后的值:" << s1.age << endl;
	
	//地址传递
	printStudent2(&s1);
	cout << "地址传递的子函数修改年龄后的值:" << s1.age << endl;
	system("pause");
	return 0;
}

06结构体中的const使用

#include<iostream>;
using namespace std;
//使用C++中的字符串要包含这个头文件
#include<string>;

//用const来防止误操作


//定义学生结构体
struct Student
{
	//成员变量
	string name;
	int age;
	int score;
};

//将函数中的形参改为指针,可以减少内存空间,而且不会复制副本出来
void printStudent1(const struct Student *s){
	//加上const之后只可以对成员变量进行读操作,不可以进行更改
	//s->age = 22;  //错误表达式

	cout << "姓名:" << s->name << " 年龄:" << s->age << " 分数:" << s->score << endl;
}

int main(){
	//创建学生结构体变量
	struct Student s1 = { "AISMALL_01", 18, 150 };

	//使用地址传递有一个弊端,在是传递中,在子函数中修改成员变量的值(指误修改)不会影响实体中的值,但是地址传递就不一样
	printStudent1(&s1);
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彤彤的小跟班

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值