栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++学习——结构体

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++学习——结构体

1.结构体基本概念

概念:

结构体属于用户自定义的数据类型,允许用户自定义的数据类型

结构体定义和使用:

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

创建方式3种:

(1)创建学生数据类型:学生(姓名、年龄、分数)

//自定义数据类型,一些类型的集合组成的数据类型

//语法: struct 类型名称 {成员列表}

//此处struct关键字不可以省略
struct Student
{
	//成员列表
	string name;
	int age;
	int score;
};//注意不能忘记分号

(2)通过学生类型创建具体学生//此处struct关键字可以省略

//2.1 struct Student s1

struct Student s1;
	s1.name="zhangsan";
	s1.age=19;
	s1.score=90;
cout<<"name:"<,否则第三个<<会报错

//2.2 struct Student s2={....}

struct Student s2={"lisi",18,70};
	cout<<"name:"< 

(3)定义结构体时顺便定义结构体变量

struct Student
{
//成员列表
	string name;
	int age;
	int score;
}s3;

2.结构体数组

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

步骤

1)定义结构体

2)主函数中创建结构体数组

3)给结构体数组中的元素赋值

4)遍历结构体数组

#include
using namespace std;
#include

//1.定义结构体
struct Student
{
	string name;
	int age;
	int score;
};


int main()
{
//2.创建结构体数组
	struct Student stuArray[3]=
	{
		{"zhangsan",18,100},{"lisi",19.80},{"wangwu",20,70}
	};
//3.给结构体数组中的元素赋值
	stuArray[2].name="zhaoliu";//修改
	stuArray[2].age=13;
	stuArray[2].score=60;

//4.遍历结构体数组
	for(int i=0;i<3;i++)
	{
		cout<<"name:"< 

 

3结构体指针

用操作符->访问结构体属性

步骤:

创建学生结构体变量

通过指针指向结构体变量

通过指针访问结构体变量中的数据

#include
using namespace std;
#include

//结构体指针
struct Student
{
	string name;
	int age;
	int score;
};
int main()
{
	//创建学生结构体变量
	struct Student s={"zhangsan",18,100};//struct可省略

	//通过指针指向结构体变量
	struct Student *p=&s;//s是结构体变量为Student类型,因此指针为Student类型,struct可省略

	//通过指针访问结构体变量中的数据
	cout<<"name:"<name<<"age:"<age<<"score:"<score;
	system("pause");
	return 0;
}

4结构体套用

利用一个结构体作为另外一个结构体的成员

#include
using namespace;

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=1230;
	t.name="laowang";
	t.age=50;
	t.stu.age=18;
	t.stu.name="xiaowang";
	t.stu.score=100;

	cout<<"teacher name:"< 

5结构体做函数参数

结构体做函数参数

传递方式:1.值传递,2.地址传递

#include
#include
using namespace std;


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

//打印学生信息函数
//1.值传递 
void printStudent1(struct student s)
{
	//s.age=100;
	cout<<"子函数1 姓名:"<name<<"age:"<age<<"score:"<score< 

6结构体中const使用场景

用const来防止误操作

#include
#include
using namespace std;

//const使用场景

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

void printStudent(student s)//const student*p 将函数中的形参改为指针可以减少内存,且不会有新的副本
{
	//s->age=150//加入const之后报错;加入const之后不可修改值,可以防止误操作
	cout<<"name:"< 

 

 案例一:
*学校正在做毕设项目,每名老师带领5个同学,总共有3名老师。要求如下:
设计学生和老师的结构体,
其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员,学生的成员有姓名、考试分数,创建数组存放3名老师,
通过函数给每个老师及所带的学生赋值,最终打印数据所带的学生数据。
*/
#include
#include
using namespace std;
#include
//学生结构体
struct Student
{
	string sname;
	int score;
};

//老师结构体
struct Teacher
{
	string tname;
	//学生数组
	struct Student sArray[5];
};


void allocateSpace(struct Teacher tArray[5],int len)
{
	string nameSeed="ABCDE";//类似于通配符的用法
	//给老师赋值
	for(int i=0;i 40~99  ;%60改为%61 --》40~100
			tArray[i].sArray[j].score=random;//随机
		}
	}
}

void printInfo(struct Teacher tArray[],int len)
{
	for(int i=0;i 
案例二: 
//设计一个英雄结构体,包括姓名、年龄、性别;创建结构体数组,数组中存放五名英雄,
//通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,所以中打印排序后的结果。
#include
#include
using namespace std;

//1.设计英雄结构体
struct Hero
{
	string name;
	int age;
	string sex;
};
//冒泡排序  年龄升序排列
void bubbleSort(struct Hero heroArray[],int len)
{
	for(int i=0;iheroArray[j+1].age)
			{
				struct Hero temp=heroArray[j];
				heroArray[j]=heroArray[j+1];
				heroArray[j+1]=temp;
			}
		}
	}
}

void printHero(struct Hero heroArray[],int len)
{
	for(int i=0;i 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/302526.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号