CH08_结构体


基本概念

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

定义和使用

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

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

  • struct 结构体名 变量名
  • struct 结构体名 变量名=
  • 定义结构体时顺便创建变量

示例:

#include<iostream>
using namespace std;

//定义结构
struct  Student
{
	string name;
	int age;
	string sex;
} s3;

int main() {

	//方式1(struct可省略)
	//struct Student s1;
	Student s1;
	s1.name = "张三";
	s1.age = 19;
	s1.sex = "男";

	cout << "姓名:" << s1.name << "年龄:" << s1.age << "性别:" << s1.sex << endl;;
		
	//方式2(struct可省略)
	//struct Student s2 = { "李四",20,"女" };
	Student s2 = {"李四",20,"女"};
	cout << "姓名:" << s2.name << "年龄:" << s2.age << "性别:" << s2.sex<<endl;

	//方式3
	s3.name = "王五";
	s3.age = 21;
	s3.sex = "男";
	cout << "姓名:" << s3.name << "年龄:" << s3.age << "性别:" << s3.sex<<endl;

	system("pause");
	return 0;
}

结构体数组

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

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

示例:

#include<iostream>
using namespace std;

//定义结构
struct  Student
{
	string name;
	int age;
	string sex;
};

int main() {
	

	//结构体数组
	Student stus[3] =
	{
		{"张三",18,"男"},
		{"李四",20,"男"},
		{"王五",22,"女"}
	};

	//修改数组中的元素
	stus[2].name = "赵六";
	stus[2].sex = "男";

	//遍历数组
	for (int i = 0; i < 3; i++) {
		cout << "姓名:" << stus[i].name << endl;
		cout << "年龄:" << stus[i].age << endl;
		cout << "性别:" << stus[i].sex << endl;
		cout << "------------------------" << endl;
	}

	system("pause");
	return 0;
}

结构体指针

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

语法:结构体 ->成员

示例:

#include<iostream>
using namespace std;

//定义结构
struct  Student
{
	string name;
	int age;
	string sex;
};

int main() {

	//创建学生结构对象
	Student stu = {"张三",18,"男"};

	//创建结构类型指针
	Student* p = &stu;

	//通过指针访问结构中的属性
	//p->name
	cout << "姓名:" << p->name << endl;
	cout << "年龄:" << p->age << endl;
	cout << "性别:" << p->sex << endl;

	cout << "姓名:" << (*p).name << endl;

	system("pause");
	return 0;
}

结构体嵌套

作用:结构体中的成员可以是另外一个结构体

示例:

#include<iostream>
using namespace std;

//定义学生结构
struct  Student
{
	string name;	//姓名
	int age;		//年龄
	string sex;		//性别
};

//定义小组结构
struct Group {
	string groupNmae;//组名
	Student stus[3];	//学生结构数组
};

int main() {

	Group g = { "一组",
		{
		{"张三",18,"男"},
		{"李四",20,"男"},
		{"王五",22,"女"}
		}
	 };

	cout << "组名:" << g.groupNmae << endl;
	for (int i = 0; i < 3; i++) {
		cout << "------------------------------" << endl;
		cout << "姓名:" << g.stus[i].name << endl;
		cout << "年龄:" << g.stus[i].age << endl;
		cout << "性别:" << g.stus[i].sex << endl;
		
	}

	system("pause");
	return 0;
}

结构体做函数的参数

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

传递方式:

值传递

地址传递

示例:

#include<iostream>
using namespace std;

//定义学生结构
struct  Student
{
	string name;	//姓名
	int age;		//年龄
	string sex;		//性别
};

//值传递
void fun1(Student stu) {
	stu.age = 100;
	cout << "姓名:" << stu.name << "年龄:" << stu.age << "性别:" << stu.sex << endl;

}

//引用传递
void fun2(Student* stu) {
	stu->age = 100;
	cout << "姓名:" << stu->name << "年龄:" << stu->age << "性别:" << stu->sex << endl;

}

int main() {

	Student stu = { "张三",20,"男" };

	//值传递
	//fun1(stu);

	//引用传递
	fun2(&stu);

	cout << "姓名:" << stu.name << "年龄:" << stu.age << "性别:" << stu.sex << endl;

	system("pause");
	return 0;
}

结构体中const使用场景

作用:在引用方式传递时,用const来防止误操作。(引用方式传递节约内存空间)。

示例:

#include<iostream>
using namespace std;

//定义学生结构
struct  Student
{
	string name;	//姓名
	int age;		//年龄
	string sex;		//性别
};



//引用传递
void fun2(const Student* stu) {
	//stu->age = 100; //报错:禁止更改
	cout << "姓名:" << stu->name << "年龄:" << stu->age << "性别:" << stu->sex << endl;

}

int main() {

	Student stu = { "张三",20,"男" };

	//引用传递
	fun2(&stu);

	cout << "姓名:" << stu.name << "年龄:" << stu.age << "性别:" << stu.sex << endl;

	system("pause");
	return 0;
}
posted on 2024-06-11 20:32  熊猫_大侠  阅读(13)  评论(0编辑  收藏  举报