C++学习记录---结构体

结构体的基本概念

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

结构体定义和使用

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

struct Student

    string name;
    int age;
    int score;
};

#include <iostream>
using namespace std;
#include <string>
//1.创建学生的数据类型:学生包括(姓名,年龄,分数)
//自定义数据类型,不同类型组成的一个集合
struct Student
{

	string name;
	int age;
	int score;

}s3;
//2.通过学生类型创建具体的学生
//方式1:


int main() {
	//2.1
	struct Student s1;//struct关键字可以省略
	//给s1属性赋值,通过。访问结构体变量中的属性
	s1.name = "张三";
	s1.age = 18;
	s1.score = 100;
	cout << "姓名:" << s1.name <<" 年龄:"<<s1.age<<" 分数:"<<s1.score<< endl;
	//2.2 struct 结构体名 结构体变量名={具体的属性值}
	struct Student s2 = { "李四",19,92 };
	cout << "姓名:" << s2.name << " 年龄:" << s2.age << " 分数:" << s2.score << endl;
	//2.3 在定义结构体时顺便定义一个结构体变量
	s3.name = "王五";
	s3.age = 15;
	s3.score = 78;
	cout << "姓名:" << s3.name << " 年龄:" << s3.age << " 分数:" << s3.score << endl;
	return 0;

}

结构体数组

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

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

#include <iostream>
using namespace std;
#include <string>
//结构体数组
//1.定义结构体
struct Student
{
	string name;
	int age;
	int score;
};

int main() {
//2.创建结构体数组
	struct Student stuArray[3] =
	{
		{"张三",18,100},
		{"李四",19,98 },
		{"王五",16,88 }
	};
//3.给结构体的数组中的元素赋值(修改)
	stuArray[2].name = "赵六";
	stuArray[2].age = 80;
	stuArray[2].score = 66;
//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;
#include <string>
struct Student
{
	string name;
	int age;
	int score;
};

int main() {
	//1.创建学生结构体变量
	struct Student s = { "z3",11,56 };
	//2.通过指针指向结构体变量
	Student *p = &s;
	//3.通过指针访问结构体变量中的数据,注意访问符号“->”
	cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << 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 stu;//辅导的学生
};


int main() {
	
	teacher t;
	t.id = 10099;
	t.name = "z3";
	t.age = 56;
	t.stu.name = "xw";
	t.stu.age = 22;
	t.stu.score = 87;

	cout << "老师的姓名:" << t.name << " 老师的id:" << t.id << " 老师的年龄:" << t.age
		<< "\n老师辅导的学生姓名:" << t.stu.name << " 学生年龄:" << t.stu.age << endl;

	return 0;
}

结构体做函数的参数

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

传递方式有两种:

  • 值传递
  • 地址传递
#include <iostream>
using namespace std;
#include <string>

//定义学生的结构体
struct student
{
	string name;
	int age;
	int score;
};
//打印学生信息的函数
//1.值传递
void printStudent1(struct student s) {
	cout << "子函数中打印 姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl;

}
//地址传递
void printStudent2(struct student *p) {
	cout << "子函数2中打印 姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;

}

int main() {
	//创建结构体变量
	struct student s;
	s.name = "z3";
	s.age = 14;
	s.score = 99;

	//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) {
	//s->age = 100;//加const之后,一旦有修改操作就会报错
	cout << "姓名:" << s->name << " 年龄:" << s->age << " 分数:" << s->score << endl;

}
int main() {
	//创建结构体变量
	struct student s = {"z3",18,98};
	printStudent(&s);

	cout <<"main函数中打印 姓名:"<<s.name << " 年龄:" << s.age << " 分数:" << s.score << endl;

	return 0;
}
  • 将函数的形参改为指针可以减少函数的内存空间,而且不会复制新的副本出来
  • 加const之后,一旦有修改操作就会报错

结构体案例                                                                                                                                                     

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值