【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,性别:男

猜你喜欢

转载自blog.csdn.net/duoduo_11011/article/details/130684221
今日推荐