C++ 结构体

目录

1.结构体的概念

2.结构体的定义和使用

3.结构体数组

4.结构体指针

5.结构体嵌套结构体

 6.结构体做函数参数

7.结构体中const使用场景

8.结构体案例

8.1案例1

8.2案例2


1.结构体的概念

2.结构体的定义和使用

#include<iostream>
using namespace std;


//1.创建学生数据类型:学生包括(姓名,年龄,分数)
//语法:struct 类型名称 {}
struct Student
{
	//姓名
	string name;
	//年龄
	int age;
	//分数
	double score;
};//注意分号

/*
//2.3在定义结构体时顺便创建结构体变量
struct Student
{
	//姓名
	string name;
	//年龄
	int age;
	//分数
	double score;
}s3;
*/
int main()
{
	//2.通过学生类型创建具体的学生
	//2.1 struct Student s1;//数据类型 变量名
	
	struct Student s1;//等价于Student s1
	//通过.访问结构体中的属性
	s1.name = "张三";
	s1.age = 18;
	s1.score = 95.6;
	cout << "姓名:" << s1.name << " " 
		<< "年龄:" << s1.age << " " << "分数:" << s1.score << endl;

	//2.2 struct Student s2={. . .}
	//创建结构体变量时,struct关键字可以省略
	Student s2 = { "李四",19,80 };
	cout << "姓名:" << s2.name << " " 
		<< "年龄:" << s2.age << " " << "分数:" << s2.score << endl;
	//2.3在定义结构体时顺便创建结构体变量
    
    //直接跟花括号
    Student s4{"王五",9};
	system("pause");//按任意键继续

	return 0;

}

3.结构体数组

#include<iostream>
using namespace std;

//结构体数组
//定义结构体
struct Student
{
	string name;
	int age;
	double score;
};
int main()
{
	//2.创建结构体数组
	struct Student stuArray[3] =
	{
		{"张三",18,100},
		{"李四",19,99},
		{"王五",16,66},
	};
	//也可以
	struct Student stuArray1[3];
	//3.给结构体中的元素赋值
	stuArray[2].name = "赵六";
	stuArray1[0].name = "赵四";
	//4.遍历结构体数组
	for (int i = 0; i < 3; i++)
	{
		cout << "姓名:" << stuArray[i].name << " "
			<< "年龄:" << stuArray[i].age << " "
			<< "分数:" << stuArray[i].score << endl;
	}
	system("pause");//按任意键继续

	return 0;

}

4.结构体指针

#include<iostream>
using namespace std;

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

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

	//3.通过指针访问结构体变量中的数据
	//通过结构体指针 访问结构体中的属性 需要利用 ->
	cout << "姓名:" << p->name << " "
		<< "年龄:" << p->age << " "
		<< "分数:" << p->score << endl;
	system("pause");//按任意键继续

	return 0;

}

5.结构体嵌套结构体

#include<iostream>
using namespace std;

struct student
{
	string name;//姓名
	int age;//年龄
	double score;
};
struct teacher
{
	int id;//教师编号
	string name;//姓名
	int age;//年龄
	student stu;//辅导的学生
};
int main()
{
	//结构体嵌套结构体
	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 << " "
		<< "老师辅导的学生姓名:" << t.stu.name << " "
		<< "学生年龄:" << t.stu.age << " "
		<< "分数:" << t.stu.score << endl;
	system("pause");//按任意键继续

	return 0;

}

 6.结构体做函数参数

#include<iostream>
using namespace std;

struct student
{
	string name;//姓名
	int age;//年龄
	double score;
};

//打印学生信息的函数
//1.值传递
void printStudent1(student d)
{
	d.age = 100;
	cout << "姓名:" << d.name << " "
		<< "年龄:" << d.age << " "
		<< "分数:" << d.score << endl;
}

//地址传递
void printStudent2(student *p)
{
	p->age = 200;
	cout << "地址传递" << endl;
	cout << "姓名:" << p->name << " "
		<< "年龄:" << p->age << " "
		<< "分数:" << p->score << endl;
}
int main()
{
	//结构体做函数的参数
	//将学生传入参数中,打印学生身上的所有信息

	student s;
	s.name = "张三";
	s.age = 18;
	s.score = 80;
	printStudent1(s);
	cout << "值传递后main函数中姓名:" << s.name << " "
		<< "年龄:" << s.age << " "
		<< "分数:" << s.score << endl;
	printStudent2(&s);
	cout << "地址传递main函数中姓名:" << s.name << " "
		<< "年龄:" << s.age << " "
		<< "分数:" << s.score << endl;
	system("pause");//按任意键继续

	return 0;

}

7.结构体中const使用场景

#include<iostream>
using namespace std;

//const的使用场景
struct student
{
	string name;//姓名
	int age;//年龄
	double score;
};

//将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本出来
//因为值传递,形参复制一份实参
void printStudent(const student *s)
{
	//s->age = 150;//加入const之后,一旦有修改操作就会报错,防止误操作
	cout << s->name << " " << s->age << " " << s->score << endl;
}
int main()
{
	student s = { "张三",18,70 };
	//通过函数打印结构体信息
	printStudent(&s);
	system("pause");//按任意键继续

	return 0;

}

8.结构体案例

8.1案例1

#include<iostream>
using namespace std;

struct student
{
	string sNname;
	int score;
};
struct teacher
{
	string tName;
	student sArray[5];
};

//给老师和学生的赋值
/*void allpcateSpace(teacher* p, int len)
{
	string nameSeed = "ABCDE";
	//给老师开始赋值
	for (int i = 0; i < len; i++)
	{
		p[i].tName = "Teacher_";
		p[i].tName += nameSeed[i];
		//通过循环给每名老师带的学生赋值
		for (int j = 0; j < 5; j++)
		{
			p[i].sArray[j].sNname = "Student_";
			p[i].sArray[j].sNname += nameSeed[j];
			int random=rand() % 61+40;//40+0~60+40
			p[i].sArray[j].score = random;
		}
		
	}
}*/
//与上面代码等价
void allpcateSpace(teacher* p, int len)
{
	string nameSeed = "ABCDE";
	//给老师开始赋值
	for (int i = 0; i < len; i++)
	{
		(p+i)->tName = "Teacher_";
		(p + i)->tName += nameSeed[i];
		//通过循环给每名老师带的学生赋值
		for (int j = 0; j < 5; j++)
		{
			(p + i)->sArray[j].sNname = "Student_";
			(p + i)->sArray[j].sNname += nameSeed[j];
			int random = rand() % 61 + 40;//40+0~60+40
			(p + i)->sArray[j].score = random;
		}

	}
}

//打印
void printInfo(teacher p[], int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << "老师姓名:" << p[i].tName << endl;
		for (int j = 0; j < 5; j++)
		{
			cout << "\t学生姓名:" << p[i].sArray[j].sNname << " "
				<< "分数:" << p[i].sArray[j].score << endl;
		}
	}
}
int main()
{
	//随机数种子
	srand((unsigned int)time(NULL));

	//1.创建3名老师的数组
	teacher tArray[3];
	//数组长度
	int len = sizeof(tArray) / sizeof(tArray[0]);
	//2.通过函数给三名老师的信息赋值,并给老师带的学生赋值
	allpcateSpace(tArray, len);
	printInfo(tArray, len);
	//3.打印
	system("pause");//按任意键继续

	return 0;

}

8.2案例2

#include<iostream>
using namespace std;

struct hero
{
	string name;
	int age;
	string sex;
};

void bubbleSort(hero* man,int len)
{
	hero temp;
	for (int i = 0; i < len - 1; i++)
	{
		for (int j = 0; j < len - i - 1; j++)
		{
			if (man[j].age > man[j + 1].age)
			{
				temp = man[j];
				man[j] = man[j + 1];
				man[j + 1] = temp;
			}
		}
	}
}

void pringMan(hero man[], int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << man[i].name << " " << man[i].age << " " << man[i].sex<< endl;
	}
}
int main()
{
	hero man[5] =
	{
		{"刘备",23,"男"},
		{"关羽",22,"男"},
		{"张飞",20,"男"},
		{"赵云",21,"男"},
		{"貂蝉",19,"女"}
	};
	int len = sizeof(man) / sizeof(man[0]);
	cout << "排序前" << endl;
	for (int i = 0; i < len; i++)
	{
		cout << man[i].name << " " << man[i].age << " " << man[i].sex << endl;
	}
	bubbleSort(man, len);
	cout << "排序后" << endl;
	pringMan(man,  len);
	system("pause");//按任意键继续

	return 0;

}

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值