【C++ 入坑指南】(12)结构体

在这里插入图片描述

一、概念

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

1.1 定义

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

示例

struct Student
{
	string name;

	int age;

	int score;
};

1.2 使用

int main()
{
	// 结构体的使用,方式一
	Student s1;
	s1.name = "Kevin";
	s1.age = 18;
	s1.score = 100;

	cout << "学生姓名:" << s1.name << ",年龄:" << s1.age << ",分数:" << s1.score << endl;

	// 结构体的使用,方式二
	Student s2 = { "James",17,90 };
	cout << "学生姓名:" << s2.name << ",年龄:" << s2.age << ",分数:" << s2.score << endl;

	return 0;
}

二、结构体数组

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

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

示例

#include <iostream>
#include <string>

using namespace std;

struct Student
{
	string name;

	int age;

	int score;
};

void printArray(Student* arr, int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << "名字:" << arr[i].name 
			 << "年龄:" << arr[i].age
			 << "分数:" << arr[i].score
			 << endl;
	}
}

int main()
{
	// 结构体的使用,方式一
	struct Student stuArr[3] = {
		{"Kevin",18,100},
		{"Bob",19,90},
		{"Steven",18,80}
	};
	
	printArray(stuArr, 3);
	return 0;
}

三、结构体指针

作用:通过指针访问结构体中的成员。
利用操作符 -> 可以通过结构体指针访问结构体属性。

示例

#include <iostream>
#include <string>

using namespace std;

struct Student
{
	string name;

	int age;

	int score;
};


int main()
{
	struct Student stu = { "Kevin",18,100 };
	
	struct Student* p = &stu;

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

	return 0;
}

四、结构体函数

示例

#include <iostream>
#include <string>

using namespace std;

struct Student
{
	string name;

	int age;

	int score;
};

// 值传递
void printInfo(Student stu)
{
	stu.age = 30;
	cout << "子函数 1 姓名:" << stu.name << ",年龄:" << stu.age << ",分数:" << stu.score << endl;
}

// 地址传递
void printInfo2(Student* p)
{
	p->age = 50;

	cout << "子函数 2 姓名:" << p->name << ",年龄:" << p->age << ",分数:" << p->score << endl;
}


int main()
{
	struct Student stu = { "Kevin",18,100 };

	printInfo(stu);

	printInfo2(&stu);

	cout << "主函数姓名:" << stu.name << ",年龄:" << stu.age << ",分数:" << stu.score << endl;

	return 0;
}

输出结果:

子函数 1 姓名:Kevin,年龄:30,分数:100
子函数 2 姓名:Kevin,年龄:50,分数:100
主函数姓名:Kevin,年龄:50,分数:100

五、实例

提目描述:设计一个英雄的结构体,包括姓名、年龄、性别;创建结构体数组,数组中存放5名英雄;通过冒泡排序算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果。

{
		{"项羽",29,"男"},
		{"凯",28,"男"},
		{"李元芳",14,"男"},
		{"王昭君",18,"女"},
		{"孙尚香",21,"女"},
}
#include <iostream>
#include <string>

using namespace std;

struct Hero
{
	string name;

	int age;

	string sex;
};

void bobSort(Hero arr[], int len)
{
	for (int i = 0; i < len - 1; i++)
	{
		for (int j = 0; j < len - i - 1; j++)
		{
			if (arr[j].age > arr[j + 1].age)
			{
				Hero temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
}


int main()
{
	struct Hero heroArray[] = {
		{"项羽",29,"男"},
		{"凯",28,"男"},
		{"李元芳",14,"男"},
		{"王昭君",18,"女"},
		{"孙尚香",21,"女"},
	};

	int len = sizeof(heroArray) / sizeof(heroArray[0]);

	bobSort(heroArray, len);

	for (int i = 0; i < len; i++)
	{
		cout << "英雄名字:" << heroArray[i].name
			<< ",年龄:" << heroArray[i].age
			<< ",性别:" << heroArray[i].sex << endl;
	}

	return 0;
}

输出结果:

英雄名字:李元芳,年龄:14,性别:男
英雄名字:王昭君,年龄:18,性别:女
英雄名字:孙尚香,年龄:21,性别:女
英雄名字:凯,年龄:28,性别:男
英雄名字:项羽,年龄:29,性别:男
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Kevin-Dev

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

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

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

打赏作者

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

抵扣说明:

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

余额充值