IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++基础入门-08结构体 -> 正文阅读

[C++知识库]C++基础入门-08结构体

8.1 结构体基本概念

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

8.2 结构体定义和使用

1、定义结构体的语法:struct 结构体名 {结构体成员列表};

例子:

//创建学生数据类型  : 学生包括(姓名,年龄,分数)
//语法:struct 类型名称 { 结构体成员列表 };
struct student
{
	//成员列表

	//姓名
	string name;
	//年龄
	int age;
	//分数
	float score;

};

2、创建结构体变量的方式
创建结构体变量的方式有三种:(推荐第一种和第二种)

  • struct 结构体名 变量名;
  • struct 结构体名 变量名={成员1值, 成员2值,…};
  • 定义结构体时顺便创建变量。
    (1)struct 结构体名 变量名;
    例子:
	struct student s1;
	//给s1的属性赋值,通过 . 来访问结构体变量中的属性
	s1.name = "张三";
	s1.age = 18;
	s1.score = 100;

(2)struct 结构体名 变量名={成员1值, 成员2值,…};
例子:

struct student s2 = { "李四", 19,80 };

(3)定义结构体时顺便创建变量
例子:

struct student
{

	string name;
	int age;
	float score;

}s3;//顺便创建结构体变量

3、使用结构体变量
通过在结构体变量后面加上 ? \cdot ? 来访问结构体变量中的属性。
例子:

	s1.name = "张三";
	s1.age = 18;
	s1.score = 100;

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

注意:在输出sring类型的数据时,在头文件处要包含#include< string >

示例:

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

//1、创建学生数据类型  : 学生包括(姓名,年龄,分数)
//自定义数据类型,一些数据类型集合组成的一个类型
//语法:struct 类型名称 { 结构体成员列表 };
struct student
{
	//成员列表

	//姓名
	string name;
	//年龄
	int age;
	//分数
	float score;

}s3;//顺便创建结构体变量

//2、通过学生类型创建具体学生


int main()
{
	//2.1 struct student s1;
	struct student s1;
		//给s1的属性赋值,通过 . 来访问结构体变量中的属性
	s1.name = "张三";
	s1.age = 18;
	s1.score = 100;

	cout << "姓名:" << s1.name << " 年龄:" << s1.age << " 分数:" << s1.score << endl;//这里输出了string类型,记得包含#include<string>头文件。

	//2.2 struct student s1={....};
	struct student s2 = { "李四", 19,80 };
	cout << "姓名:" << s2.name << " 年龄:" << s2.age << " 分数:" << s2.score << endl;

	//2.3 在定义结构体时,顺便创建结构体变量。
	s3.name = "王五";
	s3.age = 20;
	s3.score = 60;
	cout << "姓名:" << s3.name << " 年龄:" << s3.age << " 分数:" << s3.score << endl;

	//推荐第一种和第二种
	//在定义结构体时,struct关键字不可以省略;在创建结构体变量时,struct关键字可以省略,如直接写成: student s1;
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述
4、总结
(1)定义结构体时,关键字struct不可以省略;创建结构体变量时,关键字struct可以省略。
(2)结构体变量用操作符 " . " 访问成员。

8.3 结构体数组

1、作用:将自定义的结构体放入到数组中方便维护。
2、语法:struct 结构体名 数组名 [元素个数]={ { } , { } ,… { } };
例子:
在定义好“student”这个结构体后
(1)创建结构体数组

	struct student stuArry[3]=
	{
		{"张三",18,100},
		{"李四",28,99},
		{"王五",38,66}
	};

(2)给结构体中的元素赋值

	stuArry[2].name = "赵六";
	stuArry[2].age = 80;
	stuArry[2].score = 60;

(3)遍历结构体

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

示例:

#include<iostream>
#include<string>
using namespace std;
//结构体数组
//1、定义结构体
struct student
{
	string name;
	int age;
	float score;
};


int main()
{
	//2、创建结构体数组
	struct student stuArry[3]=
	{
		{"张三",18,100},
		{"李四",28,99},
		{"王五",38,66}
	};
	//3、给结构体数组中的元素赋值
	stuArry[2].name = "赵六";
	stuArry[2].age = 80;
	stuArry[2].score = 60;
	//4、遍历结构体数组
	for (int i = 0; i < 3; i++)
	{
		cout << "姓名:" << stuArry[i].name 
			 << " 年龄:" << stuArry[i].age 
			 << " 分数:" << stuArry[i].score << endl;
	}

	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述

8.4 结构体指针

1、作用:通过指针访问结构体中的成员。
利用操作符 “ -> ” ,可以通过结构体指针访问结构体属性。
例子:

在定义了 “ student "这个结构体和创建了结构体变量struct student s = { “张三”,18,100 },后,定义结构体指针:

struct student * p = &s;
//结构体指针指向结构体变量s

示例:

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

//结构体指针
//1、定义学生结构体
struct student
{
	string name;
	int age;
	float score;
};

int  main()
{
	//2、创建学生结构体变量
	struct student s = { "张三",18,100 };

	//3、通过指针指向结构体变量
	struct student * p = &s;

	//4、通过指针访问结构体变量中的数据
	//通过结构体指针,访问结构体中的属性,需要利用 '->'符号。
	cout << "姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl;
	cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;


	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述

8.5 结构体嵌套结构体

作用:结构体的成员可以是另一个结构体。
例如:每个老师辅导一个学员,一个老师的结构体中,记录了一个学生的结构体。
例子:

1、定义学生结构体

struct student
{
	string name;//学生姓名
	int age;//学生年龄
	float score;//学生分数
};

2、定义老师结构体

struct teacher
{
	int id;//教师编号
	string name;//教师姓名
	int age;//年龄
	struct student stu;//辅导的学生
};

3、在主函数之中创建老师

	struct teacher t;
	t.id = 10000;
	t.name = "老王";
	t.age = 50;
	t.stu.name = "小王";
	t.stu.age = 20;
	t.stu.score = 60;

完整示例:

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

//定义学生结构体
struct student
{
	string name;//学生姓名
	int age;//学生年龄
	float score;//学生分数
};

//定义老师结构体
struct teacher
{
	int id;//教师编号
	string name;//教师姓名
	int age;//年龄
	struct student stu;//辅导的学生
};
int main()
{
	//结构体嵌套结构体
	//创建老师
	struct teacher t;
	t.id = 10000;
	t.name = "老王";
	t.age = 50;
	t.stu.name = "小王";
	t.stu.age = 20;
	t.stu.score = 60;

	cout << "老师的姓名:" << t.name
		<< " 老师的编号:" << t.id
		<< " 老师的年龄:" << t.age
		<< endl;
	cout<< "老师辅导的学生姓名: " << t.stu.name 
		<< " 老师辅导学生的年龄: " << t.stu.age
		<< " 老师辅导学生的分数: " << t.stu.score
		<< endl;
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述

8.6 结构体做函数参数

作用:将结构体作为参数向函数中传递。
传递方式有两种:

  • 值传递
  • 地址传递

前提条件:
定义了“student”的这一结构体,并且在主函数中,定义了一个 student类型的变量,变量名为stu,而且还进行了赋值。
(1)定义“student”的这一结构体

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

(2)在main()函数中,定义并赋值了一个 student类型的结构体变量

struct student s;
	s.name = "张三";
	s.age = 20;
	s.score = 80;

要创建打印学生信息的函数

8.6.1 值传递

//1 值传递
void printstudent1(struct student s)
{
	s.age = 100;
	cout << "在子函数1中打印 姓名:" << s.name << "  年龄:" << s.age << "  考试分数: " << s.score << endl;
}

在main()输入定义好的结构体变量:

int main()
{
	struct student s;
	s.name = "张三";
	s.age = 20;
	s.score = 80printstudent1(s);
	cout << "在main()函数中打印 姓名:" << s.name 
		<< "  年龄:" << s.age 
		<< "  考试分数: " << s.score 
		<< endl;
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述
可以看到,值传递只是在子函数中改变了形参的值,将年龄变为100,而实参的值并没有发生改变,依旧是20。

8.6.2 地址传递

// 2 地址传递
void printstudent2(struct student *p)
{
	p->age = 100;
	cout << "在子函数2中打印 姓名:" << p->name << "  年龄:" <<p->age << "  考试分数: " <<p->score << endl;
}

在main()函数中输入定义好的结构体变量的地址:

int main()
{
	struct student s;
	s.name = "张三";
	s.age = 20;
	s.score = 80;
	
	printstudent2(&s);
	
	cout << "在main()函数中打印 姓名:" << s.name
	 	<< "  年龄:" << s.age
		<< "  考试分数: " << s.score
	 	<< endl;

	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述
可以看到,地址传递不仅在子函数中改变了形参的值,将年龄从20变为100,也将主函数中实参的值也进行了改变。

完整示例:

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

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

//3、创建打印学生信息的函数
//3.1 值传递
void printstudent1(struct student s)
{
	s.age = 100;
	cout << "在子函数1中打印 姓名:" << s.name << "  年龄:" << s.age << "  考试分数: " << s.score << endl;
}
//3.2 地址传递
void printstudent2(struct student *p)
{
	p->age = 100;
	cout << "在子函数2中打印 姓名:" << p->name << "  年龄:" <<p->age << "  考试分数: " <<p->score << endl;
}

int main()
{
	//结构体做函数参数
	//将学生传入到一个参数中,打印学生身上的所有信息
	//2、创建结构体变量
	struct student s;
	s.name = "张三";
	s.age = 20;
	s.score = 80;

	//printstudent1(s);
	printstudent2(&s);
	
	cout << "在main()函数中打印 姓名:" << s.name << "  年龄:" << s.age << "  考试分数: " << s.score << endl;


	system("pause");
	return 0;
}

总结:如果不想修改主函数当中的数据,用值传递,反之则用地址传递。

8.7 结构体中const的使用场景

作用:用const防止误操作。

前提:定义好了”student"的结构体

定义了一个常量指针,如下:

void printstudent(const struct student *s)
{
}

在这里插入图片描述
可以看到,在定义了常量指针后,不能对参数进行修改,否则报错。

完整示例:

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

//const使用场景
struct student
{
	string name;
	int age;
	float score;
};
//将函数中1的形参改为指针,可以减少内存,而且不会复制新的副本
void printstudent(const struct student *s)
{
	//s->age = 120;//加入const之和,一旦有修改的操作就会报错,可以防止我们的误操作
	cout << "姓名:" << s->name << " 年龄:" << s->age << " 分数:" << s->score << endl;
}

int main()
{
	//创建结构体变量
	struct student s = { "张三",15,70 };

	//通过函数打印结构体变量信息
	printstudent(&s);
	system("pause");
	return 0;
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-07-10 14:21:50  更:2021-07-10 14:22:48 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/4 10:45:42-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码