C++学习之旅 - 结构体

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

结构体定义和使用

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

定义

//1. 创建学生数据类型: 学生包括(姓名,年龄,分数)
struct Student {
    
    
  //成员列表
	string name;
	int age;
	int score;
};

使用

  1. struct Student s1;
  2. struct Student s2 = {....};
#include <iostream>

using namespace std;

//1. 创建学生数据类型: 学生包括(姓名,年龄,分数)
struct Student {
    
    
  //成员列表
	string name;
	int age;
	int score;
};

int main(void) {
    
    
	struct Student s1;
	//通过结构体变量.变量中的属性
	s1.name = "李四";
	s1.age = 18;
	s1.score = 90;
	cout << "学生1姓名:" << s1.name << "年龄:" << s1.age << "分数:" << s1.score << endl;

	struct Student s2 = {
    
     "王五",19,95 };
	cout << "学生2姓名:" << s2.name << "年龄:" << s2.age << "分数:" << s2.score << endl;
}

结构体数组

学生量比较大的时候就可以考虑放入一个数组中

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

#include <iostream>

using namespace std;

//1. 创建学生数据类型: 学生包括(姓名,年龄,分数)
struct Student {
    
    
  //成员列表
	string name;
	int age;
	int score;
};

int main(void) {
    
    
	struct Student stuArray[3] = {
    
     
		{
    
    "张三",18,80},
		{
    
    "李四",19,88},
		{
    
    "王五",19,79}
	};

	//修改元素
	stuArray[2].name = "赵四";
	stuArray[2].age = 20;
	stuArray[2].score = 60;
	for (int i = 0; i < 3; i++) {
    
    
		cout << "学生1姓名:" << stuArray[i].name << "年龄:" << stuArray[i].age << "分数:" << stuArray[i].score << endl;
	}
}

结构体指针

#include <iostream>

using namespace std;

//1. 创建学生数据类型: 学生包括(姓名,年龄,分数)
struct Student {
    
    
  //成员列表
	string name;
	int age;
	int score;
};

int main(void) {
    
    
	//1. 创建学生结构体变量
	struct Student s = {
    
     "张三",18,100 };
	//2. 通过指针指向结构体变量
	Student* p = &s;
	//3. 通过指针访问结构体变量中的数据
	cout << p->name << endl;
}

结构体嵌套结构体

#include <iostream>

using namespace std;

//1. 创建学生数据类型: 学生包括(姓名,年龄,分数)
struct student {
    
    
  //成员列表
	string name;
	int age;
	int score;
};
struct teacher {
    
    
	int id;
	string name;
	student stu;
};

int main(void) {
    
    
	student s1 = {
    
     "张三",18,65 };
	teacher t1 = {
    
     0,"王老师",s1 };
	cout << "老师的名称:" << t1.name << "带的学生名字:" << t1.stu.name << endl;
}

结构体做函数参数

值传递

#include <iostream>

using namespace std;

//1. 创建学生数据类型: 学生包括(姓名,年龄,分数)
struct student {
    
    
  //成员列表
	string name;
	int age;
	int score;
};

void printStudent(student stu) {
    
    
	cout << stu.name << endl;
}

int main(void) {
    
    
	student s1 = {
    
    "张三",16,91};
	printStudent(s1);
}

地址传递

#include <iostream>

using namespace std;

//1. 创建学生数据类型: 学生包括(姓名,年龄,分数)
struct student {
    
    
  //成员列表
	string name;
	int age;
	int score;
};

//将形参改为指针,可以减少内存空间
void printStudent(student *stu) {
    
    
	cout << stu.name << endl;
}
void setStudentName(student* stu,string name) {
    
    
	stu->name = name;
}

int main(void) {
    
    
	student s1 = {
    
    "张三",16,91};
	printStudent(&s1);
	setStudentName(&s1,"李四");
	printStudent(&s1);
}

在这里插入图片描述

结构体中const使用场景

这个防止我们误操作
其实就是防止我们有写的操作

#include <iostream>

using namespace std;

//1. 创建学生数据类型: 学生包括(姓名,年龄,分数)
struct student {
    
    
  //成员列表
	string name;
	int age;
	int score;
};

void printStudent(student stu) {
    
    
	cout << stu.name << endl;
}
void setStudentName(student* stu,string name) {
    
    
	stu->name = name;
}

int main(void) {
    
    
	const student s1 = {
    
    "张三",16,91}; //加上了const
	printStudent(s1);
	setStudentName(&s1,"李四");
	printStudent(s1);
}

在这里插入图片描述

一旦有改变元素的行为就会报错

猜你喜欢

转载自blog.csdn.net/yasinawolaopo/article/details/131050301