C++入门学习---结构体

1.结构体概念

在C++中,结构体(struct)是用户自定义的一种复合数据类型,用于将不同类型的数据组合成一个整体。C++的结构体继承了C语言的struct,但功能更强大,几乎与类(class)相同,主要区别在于默认访问权限。

2.结构体定义和使用

语法:

复制代码
struct 结构体名 { 结构体成员列表 };

C++中的结构体定义如下所示:

复制代码
#include <iostream>
#include <string>
using namespace std;

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

int main() {
    Student s1;                // 创建结构体变量
    s1.id = 1001;
    s1.name = "张三";
    s1.age = 20;

    // 也可以用聚合初始化(C++推荐)
    Student s2 = { 1002, "李四", 21 };

    // C++11起支持列表初始化
    Student s3{ 1003, "王五", 22 };

    cout << s1.name << " " << s1.age << endl;
    cout << s2.name << " " << s2.age << endl;
    cout << s3.name << " " << s2.age << endl;

}

3.结构体数组

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

复制代码
struct  结构体名 数组名[元素个数] = {  {} , {} , ... {} }

#include <iostream>
#include <string>
using namespace std;

//结构体定义
struct student
{
	//成员列表
	string name;  //姓名
	int age;      //年龄
	int score;    //分数
};

int main() {

	//结构体数组
	struct student stu[3] =
	{
		{"张三",18,80 },
		{"李四",19,60 },
		{"王五",20,70 }
	};

	for (int i = 0; i < 3; i++)
	{
		cout << "姓名:" << stu[i].name << " 年龄:" << stu[i].age << " 分数:" << stu[i].score << endl;
	}

	system("pause");

	return 0;
}

4.结构体指针

通过结构体的指针来访问结构体内的成员变量,利用操作符 -> 可以通过结构体指针访问结构体属性

结构体指针定义

复制代码
struct 结构体名称  * 指针名称 = 结构体的引用

//完整示例
struct student stu = { "张三",18,100, };
struct student * p= &stu;

//结构体定义
struct student
{
	//成员列表
	string name;  //姓名
	int age;      //年龄
	int score;    //分数
};

int main() {

	struct student stu = { "张三",18,100, };

	struct student* p = &stu;

	p->name="李四"; //指针通过 -> 操作符可以访问成员
	p->age = 20;

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

	system("pause");

	return 0;
}

5.结构体嵌套

在结构体中,一个结构体中的成员可以是另一个结构体,例如每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体

复制代码
//学生结构体定义
struct student
{
	//成员列表
	string name;  //姓名
	int age;      //年龄
	int score;    //分数
};

//教师结构体定义
struct teacher
{
    //成员列表
	int id; //职工编号
	string name;  //教师姓名
	int age;   //教师年龄
	struct student stu; //子结构体 学生
};


int main() {

	struct teacher t1;
	t1.id = 1001;
	t1.name = "李老师";
	t1.age = 36;

	t1.stu.name = "李四";
	t1.stu.age = 19;
	t1.stu.score = 95;

	cout << "教师 职工编号: " << t1.id << " 姓名: " << t1.name << " 年龄: " << t1.age << endl;
	
	cout << "辅导学员 姓名: " << t1.stu.name << " 年龄:" << t1.stu.age << " 考试分数: " << t1.stu.score << endl;

	system("pause");

	return 0;
}

6.结构体作为函数参数

在C++的函数中,传递方式有两种:

值传递
地址传递

在C++中可以将结构体作为参数进行传递

复制代码
#include <iostream>
#include <string>
using namespace std;

//结构体定义
struct student
{
	//成员列表
	string name;  //姓名
	int age;      //年龄
	int score;    //分数
};

//结构体值传递
void printStudent1(student stu)
{
	stu.age = 18;
	
}

//结构体地址传递
void printStudent2(student* stu)
{
	stu->age = 18;
	
}
int main() {

	student stu = { "李四",20,96 };
	//值传递
	printStudent1(stu);
	cout << "值传递姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;

	cout << endl;

	//地址传递
	printStudent2(&stu);
	cout << "地址传递姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;

	system("pause");

	return 0;
}

7.结构体中的的const

const主要用于保证数据的不可变性、防止误操作、提升代码安全性和可读性。

当结构体变量不需要修改时,用const修饰整个对象。防止任何成员被意外更改。

结构体作为函数参数传递时,尤其是地址传递(指针),用const修饰指针,防止函数内部误操作修改原数据。

复制代码
struct Student {
    string name;
    int age;
    int score;
};

void printStudent(const Student* s) {  // const防止误修改
    // s->age = 100;  // 错误:编译器报错
    cout << "姓名:" << s->name << " 年龄:" << s->age << endl;
}

int main() {
    Student s = {"张三", 20, 90};
    printStudent(&s);  // 原数据不变
}
相关推荐
遇见~未来9 分钟前
JavaScript构造函数与Class终极指南
开发语言·javascript·原型模式
weixin_4617694014 分钟前
15. 三数之和
c++·算法·leetcode·三数之和
foundbug99921 分钟前
基于MATLAB的TDMP-LDPC译码器模型构建、仿真验证及定点实现
开发语言·matlab
X***078834 分钟前
从语言演进到工程实践全面解析C++在现代软件开发中的设计思想性能优势与长期生命力
java·开发语言
毕设源码-钟学长1 小时前
【开题答辩全过程】以 基于Python的车辆管理系统为例,包含答辩的问题和答案
开发语言·python
报错小能手1 小时前
线程池学习(七)实现定时(调度)线程池
学习
CCPC不拿奖不改名1 小时前
数据处理与分析:数据可视化的面试习题
开发语言·python·信息可视化·面试·职场和发展
液态不合群1 小时前
线程池和高并发
开发语言·python
小镇学者1 小时前
【c++】C++字符串删除末尾字符的三种实现方法
java·开发语言·c++
SmartRadio1 小时前
在CH585M代码中如何精细化配置PMU(电源管理单元)和RAM保留
linux·c语言·开发语言·人工智能·单片机·嵌入式硬件·lora