C++结构体

结构体

结构体基本概念

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

结构体中的数据成员可以是基本数据类型(如 int、float、char 等),也可以是其他结构体类型、指针类型等。

结构体的定义与使用

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

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

  • struct 结构体名 变量名
  • struct 结构体名 变量名 = { 成员1值, 成员2值,…};
  • 定义结构体是顺便创建变量

exmple:

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

  struct student
  {
      //成员列表
      string name;//姓名
      int age;    //年龄
      int score;  //分数
  }stu3;//结构体变量创建方式 3

  int main()
  {
      //结构体变量创建方式 1
      struct student stu1;//struct关键字可以省略

      stu1.name = "张三";
      stu1.age = 18;
      stu1.score = 90;

      cout << "姓名:" << stu1.name << "年龄:" << stu1.age << "分数:" << stu1.score << endl;

      //结构体变量创建方式 2
      struct student stu2 = { "李四",19,80 };

      cout << "姓名:" << stu2.name << "年龄:" << stu2.age << "分数:" << stu2.score << endl;

      stu3.name = "王五";
      stu3.age = 20;
      stu3.score = 90;

      cout << "姓名:" << stu3.name << "年龄:" << stu3.age << "分数:" << stu3.score << endl;

      system("pause");
      return 0;
  }

结构体用 . 来访问成员!!!

结构体数组

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

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

示例

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

//1.定义结构体
struct Student
{
	string name;
	int age;
	int score;
};
int main()
{
	//2.创建结构体数组
	struct Student stuArray[3] =
	{
		{"张三",18,100},
		{"李四",19,90},
		{"赵五",18,98}
	};
	//3.给结构体数组中的元素赋值
	stuArray[2].name = "赵六";
	stuArray[2].age = 80;
	stuArray[2].score = 60;

	//4.遍历结构体数组
	for (int i = 0;i < 3;i++)
	{
		cout << "姓名:" << stuArray[i].name << "年龄:" << stuArray[i].age << "分数:" << stuArray[i].score << endl;
    }

	system("pause");
	return 0;
}

结构体指针

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

利用操作符 -> 可以通过结构体指针访问结构体属性

示例:

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

//定义结构体
struct Student
{
	string name;
	int age;
	int score;
};
int main()
{
	//1.创建学生结构体变量
	struct Student s = { "张三",18,100 };

	//2.通过指针指向结构体变量
	struct Student* p = &s;

	//3.通过指针访问结构体变量中的数据
	cout << "姓名:" << p->name << "年龄:" << p->age << "分数:" << p->score << endl;

	system("pause");
	return 0;
}

结构体的嵌套

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

例如:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体

示例:

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

struct student
{
	string name;
	int age;
	int score;
};

struct teacher
{
	int id;
	string name;
	int age;
	struct student stu;//辅导的学生
};
int main()
{
	//结构体嵌套结构体
	teacher t;
	t.id = 1007;
	t.name = "老王";
	t.age = 50;
	t.stu.name = "小王";
	t.stu.age = 20;
	t.stu.score = 90;

	cout << "老师姓名:" 
		 << t.name
		 << "老师编号:" 
		 << t.id << "老师年龄:"
		 << t.age << "老师的学生姓名:" 
		 << t.stu.name << "老师的学生的年龄:"
		 << t.stu.age << "老师的学生的成绩:"
		 << t.stu.score << endl;

	system("pause");
	return 0;
}

结构体做函数参数

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

传递方式

  • 值传递
  • 地址传递

示例:

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

//学生结构体的定义
struct student
{
	//成员列表
	string name;
	int age;
	int score;
};

//值传递
void printStudent(student stu)
{
	stu.age = 20;//子函数中会变为20,但是主函数中不会
	cout << "子函数中姓名:" << stu.name << "年龄:" << stu.age << "分数:" << stu.score << endl;

}

//地址传递
void printStudent2(student *stu)
{
	stu->age = 200;//子函数和主函数均会变为200
	cout << "子函数中姓名:" << stu->name << "年龄:" << stu->age << "分数:" << stu->score << endl;

}
int main()
{
	student stu = { "张三",18,85 };
	//值传递
	printStudent(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;
}

注意:如果不想修改主函数中的数据,用值传递;反之,用地址传递。

结构体中const使用场景

**作用:**用const来防止误操作

示例:

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

//学生结构体定义
struct student
{
	string name;
	int age;
	int score;
};

//const使用场景
//将函数中的形参改为指针,可以减少内存空间,而且不会复制出新的副本
void printStudent(const student* stu)
{
	//stu->age = 100;//操作会失败,因为加了const修饰
	cout << "姓名:" << stu->name << "年龄:" << stu->age << "分数:" << stu->score << endl;

}
int main()
{
	struct student stu = { "张三",18,100 };

	printStudent(&stu);

	system("pause");
	return 0;
}
  • 14
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值