C++结构体基本知识

目录

1. 结构体基本概念

2. 结构体的定义和使用

3. 结构体数组

4. 结构体指针

5. 结构体嵌套结构体

6. 结构体做函数参数

7. 结构体中使用const

8. 综合使用案例


1. 结构体基本概念

用户自定义的数据类型,用于存储不同的数据类型。

2. 结构体的定义和使用

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

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

Struct 结构体名 变量名

Struct 结构体名 变量名 = {成员1值,成员2值,…}

定义结构体时顺便创建变量

下面通过代码来说明

#include<iostream>
#include<vector>
#include<array>
using namespace std;

struct student {
	//成员列表

	//姓名
	string name;
	//年龄
	int age;
	//分数
	int scores;
}s3;

int main()
{
	//Struct 结构体名 变量名
	struct student s1;
	s1.name = "zhangsan";  //给s1属性赋值,通过.访问结构体变量中的属性
	s1.age = 18;
	s1.scores = 100;
	cout << "name: " << s1.name << "  age: " << s1.age << "  scores: " << s1.scores << endl;
	// Struct 结构体名 变量名 = { 成员1值,成员2值,… }
	struct student s2 = { "Lisi", 19, 80 };
	cout << "name: " << s2.name << "  age: " << s2.age << "  scores: " << s2.scores << endl;
	//定义结构体时顺便创建变量
	s3.name = "wangwu";  //给s1属性赋值,通过.访问结构体变量中的属性
	s3.age = 18;
	s3.scores = 100;
	cout << "name: " << s3.name << "  age: " << s3.age << "  scores: " << s3.scores << endl;
	
	student s4; // 创建结构体变量的时候关键字struct可以省略,创建结构体时不可以省略。
	
	return 0;
}

3. 结构体数组

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

下面在代码中展示具体操作

#include<iostream>
#include<vector>
#include<array>
using namespace std;

struct student {
	//成员列表

	//姓名
	string name;
	//年龄
	int age;
	//分数
	int scores;
};


int main()
{
	//创建结构体数组
	struct student stuArray[3] =
	{
		{"ZhangSan", 18, 100},
		{"LiSi", 28, 99},
		{"WangWu", 25, 66},
	};
	//给结构体数组中的元素赋值
	stuArray[2].name = "Zhaowu";
	stuArray[2].age = 80;
	stuArray[2].scores = 60;
	// 遍历结构体数组
	for (int i = 0; i < 3; i++)
	{
		cout << "name: " << stuArray[i].name << "  age: " << stuArray[i].age
			<< "  scores: " << stuArray[i].scores << endl;
	}

	return 0;
}

4. 结构体指针

通过操作符->访问结构体属性。

下面在代码中展示具体操作

#include<iostream>
#include<vector>
#include<array>
using namespace std;

struct student {
	//姓名
	string name;
	//年龄
	int age;
	//分数
	int scores;
};


int main()
{
	//创建结构体变量
	student s = { "Lisi", 19, 80 };	

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

	//通过指针访问结构体变量中的数据
	cout << "name: " << p->name << "  age: " << p->age
		<< "  scores: " << p->scores << endl;

	return 0;
}

5. 结构体嵌套结构体

结构体的成员中可以是另一个结构体。

下面在代码中展示具体操作

#include<iostream>
#include<vector>
#include<array>
using namespace std;

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

struct teacher{
	int id;
	string name;
	int age;
	student stu;
};

int main()
{
	//创建结构体变量
	teacher t;
	t.id = 10000;
	t.name = "Wang";
	t.age = 50;
	t.stu.name = "Lisi";
	t.stu.age = 19;
	t.stu.scores = 80;

	cout << "id: " << t.id << "  age: " << t.age
		<< "  name: " << t.name << endl;
	cout << "stu_name: " << t.stu.name << "  stu_age: " << t.stu.age
		<< "  stu_score: " << t.stu.scores << endl;

	return 0;
}

6. 结构体做函数参数

将结构体作为参数向函数内传递,传递方式有两种:值传递和地址传递

下面在代码中展示具体操作

#include<iostream>
#include<vector>
#include<array>
using namespace std;

struct student {
	string name;
	int age;
	int scores;
};
// 值传递
void printStudent1(student s)
{ 
	s.age = 100;
	cout << "stu_name: " << s.name << "  stu_age: " << s.age
		<< "  stu_score: " << s.scores << endl;

}
// 地址传递
void printStudent2(student * p)
{
	p->age = 200;
	cout << "stu_name: " << p->name << "  stu_age: " << p->age
		<< "  stu_score: " << p->scores << endl;

}
int main()
{
	//创建结构体变量
	student s;
	s.name = "ZhangSan";
	s.age = 20;
	s.scores = 100;

	printStudent1(s); //值传递不会影响实参
	cout << "值传递结果:stu_name: " << s.name << "  stu_age: " << s.age
		<< "  stu_score: " << s.scores << endl;
	printStudent2(&s);//地址传递影响实参
	cout << "stu_name: " << s.name << "  stu_age: " << s.age
		<< "  stu_score: " << s.scores << endl;

	return 0;
}

7. 结构体中使用const

常量指针节省内存案例如下。

#include<iostream>
#include<vector>
#include<array>
using namespace std;

struct student {
	string name;
	int age;
	int scores;
};
// 值传递
void printStudent1(student s)
{ 
	s.age = 100;
	cout << "stu_name: " << s.name << "  stu_age: " << s.age
		<< "  stu_score: " << s.scores << endl;

}
// 地址传递
void printStudent2(student * p)
{
	p->age = 200;
	cout << "stu_name: " << p->name << "  stu_age: " << p->age
		<< "  stu_score: " << p->scores << endl;
}

void printStudent3(const student* p)
{
	// p->age = 200; 常量指针指向可以改,但是指向的数值不可以修改,会报错
	cout << "stu_name: " << p->name << "  stu_age: " << p->age
		<< "  stu_score: " << p->scores << endl;

}
int main()
{
	//创建结构体变量
	student s;
	s.name = "ZhangSan";
	s.age = 20;
	s.scores = 100;

	printStudent1(s); // 每传递一次形参就必须额外分配一次内存,如果结构体变量过多,内存占用将会非常大
	printStudent2(&s);//地址传递影响实参,权限过大
	printStudent3(&s);//使用常量指针,每次传递指针只需要占用4个字节,并且不会影响实参

	return 0;
}

8. 综合使用案例

#include<iostream>
#include<vector>
#include<array>
#include<ctime>
using namespace std;

struct student {
	string sName;
	int scores;
};

struct Teacher {
	string tName;
	struct student sArray[5];
};

void allocateSpace(struct Teacher tArray[], int len)
{
	string nameSeed = "ABCDE";
	// 对老师赋值
	for (int i = 0; i < len; i++)
	{	
		tArray[i].tName = "Teacher_";
		tArray[i].tName += nameSeed[i];
		// 对学生循环赋值
		for (int j = 0; j < 5; j++)
		{
			tArray[i].sArray[j].sName = "Student_";
			tArray[i].sArray[j].sName += nameSeed[j];
			int random = rand() % 60 + 40; // 对60取余后范围为0~59,+40后范围为40~99
			tArray[i].sArray[j].scores = random;
		}

	}
}

void printInfo(struct Teacher tArray[], int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << "Teacher name:  " << tArray[i].tName << endl;
		for (int j = 0; j < 5; j++)
		{
			cout << "\tStudent name:  " << tArray[i].sArray[j].sName;
			cout << "  Score:  " << tArray[i].sArray[j].scores << endl;
		}
	}
}
int main()
{
	// 随机数种子
	srand((unsigned int)time(NULL));
	// 创建三名老师的数组
	Teacher tArray[3];

	//通过函数对老师和学生信息赋值
	int len = sizeof(tArray) / sizeof(tArray[0]);
	allocateSpace(tArray, len);

	//打印信息
	printInfo(tArray, len);

	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值