CPP入门09:结构体

一、概念

        结构体是用户定义的数据类型,用户可以在里面存储不同数据类型。比如自己定义一个属于“人”的数据类型,里面包含年龄(整型),身高(浮点型),姓名(字符串)... ...

二、定义与使用

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

        使用:使用结构体创建变量的方式有三种

  • struct 结构体名 变量名
  • struct 结构体名 变量名 = { 成员1值 ,成员2值... ... }
  • 创建结构体时顺便创建变量

通过一个案例了解以上语法的使用:

#include<iostream>
using namespace std;

//创建一个名为student的结构体
struct student {
	string name;
	int age;
	int score;
};


int main() {
	//结构体变量创建方式一
	struct student stu1;//struct 可以省略:student stu1;
	stu1.name = "张三";
	stu1.age = 18;
	stu1.score = 100;
	cout << "姓名:" << stu1.name << " 年龄:" << stu1.age << " 分数:" << stu1.score << endl;

	//结构体变量创建方式二
	student stu2 = { "李四",19,60 };
	cout << "姓名:" << stu2.name << " 年龄:" << stu2.age << " 分数:" << stu2.score << endl;

	system("pause");
	return 0;
}
  • 定义结构体时关键字为struct,不可省略。
  • 创建结构体变量时,struct可以省略。
  • 使用"."来访问成员,如name,age,score是student的成员。

三、结构体数组

作用:将自定义的结构体放入数组中方便维护。如一个班60个学生的student数据。

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

示例:以标题二的student为例。

#include<iostream>
using namespace std;

//创建一个名为student的结构体
struct student {
	string name;
	int age;
	int score;
};


int main() {
	//创建结构体数组(按照结构体成员顺序)
	student s_arr[3] = {
		{"张三",18,100},
		{"李四",19,80},
		{"黄五",20,60}
	};

	//给结构体数组中的元素赋值
	s_arr[2].name = "王五";
	s_arr[2].age = 17;
	s_arr[2].score = 99;

	//遍历结构体数组
	for (int i = 0; i < 3; i++) {
		cout << "姓名:" << s_arr[i].name << " 年龄:" << s_arr[i].age << " 分数:" << s_arr[i].score << endl;
	}


	system("pause");
	return 0;
}

四、结构体指针

作用:通过指针访问结构体中的成员。

  • 使用操作符->通过结构体指针访问结构体属性。
  • 因为结构体成员包含多种数据类型,所以不应用int、double等创建指针。

示例:以标题三中的数组为例使用结构体指针访问数组中的变量。

#include<iostream>
using namespace std;

//创建一个名为student的结构体
struct student {
	string name;
	int age;
	int score;
};


int main() {
	//创建结构体数组(按照结构体成员顺序)
	student s_arr[3] = {
		{"张三",18,100},
		{"李四",19,80},
		{"黄五",20,60}
	};
	
	//创建结构体指针并访问变量
	student* p = &s_arr[0];
	cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;

	system("pause");
	return 0;
}

五、结构体嵌套

作用:结构体中的成员可以是另一个结构体。比如一个老师结构体中的一个成员可以是学生结构体。

示例:写一个老师结构体,其中嵌套一个学生结构体。

#include<iostream>
using namespace std;

//创建一个名为student的结构体
struct student {
	string name;
	int age;
	int score;
};
//创建名为teacher的结构体
struct teacher {
	string name;
	int age;
	int id;
	student stu;//在成员中创建结构体变量
};


int main() {
	teacher t;//创建一个老师
	//赋值
	t.id = 1;
	t.name = "王老师";
	t.age = 40;
	t.stu.name = "王同学";
	t.stu.age = 18;
	t.stu.score = 100;

	//输出信息
	cout << "老师名字:" << t.name << " 教师编号:" << t.id << " 教师年龄:" << t.age << endl;
	cout << "辅导学生姓名:" << t.stu.name << " 辅导学生年龄:" << t.stu.age << " 辅导学生成绩:" << t.stu.score << endl;

	system("pause");
	return 0;
}

六、结构体做函数参数

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

传递方式:

  • 值传递
  • 地址传递

示例:分别用值传递和地址传递打印结构体变量。

#include<iostream>
using namespace std;

//创建一个名为student的结构体
struct student {
	string name;
	int age;
	int score;
};
//值传递函数
void print_1(student stu) {
	cout << "姓名:" << stu.name << " 年龄:" << stu.age << " 分数:" << stu.score << endl;
}
//地址传递函数
void print_2(student* p) {
	cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;
}
int main() {

	student s = { "李四",19,100 };

	//值传递
	print_1(s);
	//地址传递
	print_2(&s);

	system("pause");
	return 0;
}

七、结构体中的const

作用:使用const防止误操作。例如当使用结构体指针时,可能会在想修改形参时误将实参修改,const在此可以避免此类误操作的发生。

#include<iostream>
using namespace std;

//创建一个名为student的结构体
struct student {
	string name;
	int age;
	int score;
};

//地址传递函数
void print_2(const student * p) {
	//p->age = 999999;//操作失败,const修饰不可更改
	cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;
}
int main() {
	
	student s = { "张三",19,100 };
	print_2(&s);

	system("pause");
	return 0;
}

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值