c++结构体入门

1.1结构体基本概念

结构体属于用户自定义的数据类型,允许用户往里面设置不同的数据类型(如可在里面设置int char等类型的数据)。

1.2结构体的定义和使用

语法 : struct  结构体名 {

结构体的成员

}; 

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

1.struct 结构体名 变量名

2.struct 结构体名 变量名 = {成员1值,成员2值......}

3. 定义结构体时顺便创建变量(不建议使用)

 代码展示前两种创建变量方式如:

创建一个学生结构体,里面元素有名字,年龄以及分数。

#include<iostream>
#include<cstring>
using namespace std;
struct student {
	string name;
	int age;
	int score;
};
int main() {
	struct student s1;      //这就是第一种创建方式,其中struct可省略
	s1.name = "阿毛";       //创建完后在下边进行赋值,以变量名.结构体内的变量的形式进行赋值
	s1.age = 18;
	s1.score = 80;
	cout << "名字:" << s1.name << "年龄:" << s1.age << "分数:" << s1.score << endl;
	struct student s2 = { "张三",28,90 };    //这就是第二种创建方式,struct也可以省略
	cout << "名字:" << s2.name << "年龄:" << s2.age << "分数:" << s2.score << endl;
}

1.3结构体数组

结构体数组即是在上一步创建变量的时候将这个变量改为数组,结构体数组就创建成功,具体如以下代码:

​
#include<iostream>
#include<cstring>

using namespace std;

//定义结构体
struct student {
	//成员列表
	string name;		//姓名
	int age;			//年龄
	int scoro;			//分数
};


int main()
{
	//结构体数组
	struct student arr[3] =
	{
		{ "张三", 18, 80 },
		{ "李四", 19, 60 },
		{ "王五", 18, 70 }
	};

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

​

1.4结构体指针

通过创建结构体指针去访问结构体,需要用到“->”箭头形的这个符号(减号与大于号)

代码如下:

​
#include<iostream>
#include<cstring>
using namespace std;
//全局变量声明区

//函数声明区

//结构体定义
struct student
{
	//成员列表
	string name;  //姓名
	int age;      //年龄
	int score;    //分数
};
int main()
{	
	struct student stu = { "张三", 18, 100 };

	struct student* p = &stu;

	p->score = 80;		//指针通过->操作符可以访问成员

	cout << "姓名: " << p->name << " 年龄: " << p->age << " 分数: " << p->score << endl;
	return 0;
}

​

1.5结构体嵌套结构体

顾名思义,即一个结构体里面包含着另一个结构体,这在生活中的运用十分广泛。

举个例子,一个老师有自己的教师编号、年龄以及他辅导的学生,他辅导的学生又有自己的名字、年龄以及分数,这就是在一个老师的结构体中,包含着学生这个结构体,代码实现如下:

#include<iostream>
#include<cstring>
using namespace std;
//学生结构体
struct student 
{
	//成员列表
	string name;		//姓名
	int age;			//年龄
	int score;			//分数
};

//教师结构体定义
struct teacher
{
	//成员列表
	int id;				//职工编号
	string name;		//教师姓名
	int age;			//教师年龄
	struct student stu; //子结构体 学生
};

int main()
{
	struct teacher t1;	
	t1.id = 10000;		
	t1.name = "老王";	
	t1.age = 40;
	
	t1.stu.name = "张三";
	t1.stu.age = 18;
	t1.stu.score = 100;

	cout << "教师 职工编号:" << t1.id << " 姓名: " << t1.name << " 年龄: " << t1.age << endl;

	cout << "辅导学员 姓名:" << t1.stu.name << " 年龄:" << t1.stu.age << " 考试分数: " << t1.stu.score << endl;
	return 0;
}

1.6 结构体做函数参数

结构体做函数参数,意思就是把结构体的元素值传入到函数当中去,这里有两种方法:

1.值传递

即定义一个函数,直接将结构体的值传入到函数当中去;

!!!注意,这时在函数中对结构体变量元素值进行修改,并不会修改函数之外的参数值,即形参值改变不影响实参的值。

2.地址传递

即定义的函数,通过传回指针的方式来实现;

!!!注意,这时在函数中对结构体变量元素值进行修改,会修改函数之外的参数值,这个时候在函数中修改变量的值,实际就是修改了变量地址上对应的值,会改变主函数中对应参数的值。

代码如下

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

//学生结构体定义
struct student
{
	string name;
	int age;
	int score;
};
//函数声明
void printfStudent(student stu);
void printfStudent2(student* stu);

int main()
{
	student stu = { "张三", 18, 100 };
	//值传递
	printfStudent(stu);
	cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;

	cout << endl;

	//地址传递
	printfStudent2(&stu);
	cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;
	return 0;
}

//值传递
void printfStudent(student stu)
{
	stu.age = 28;
	cout << "子函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;
}

//地址传递
void printfStudent2(student* stu)
{
	stu->age = 28;
	cout << "子函数中 姓名:" << stu->name << " 年龄: " << stu->age << " 分数:" << stu->score << endl;

}

1.7const在结构体中的应用

const在结构体的应用大多数扮演的是一个防止失误操作的角色,见代码讲解:

#include<iostream>
#include<cstring>
using namespace std;
//学生结构体定义
struct student
{
	//成员列表
	string name;  //姓名
	int age;      //年龄
	int score;    //分数
};

void printStudent(const student* stu)
{

	//stu->age = 100;		无法进行该操作因为加了const修饰,该值无法被修改
	cout << "姓名:" << stu->name << " 年龄:" << stu->age << " 分数:" << stu->score << endl;
}

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

	printStudent(&stu);

	return 0;
}

1.8结构体应用案例

案例描述:
设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放5名英雄。
通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果。
五名英雄信息如下:
        {"刘备",23,"男"},
        {"关羽",22,"男"},
        {"张飞",20,"男"},
        {"赵云",21,"男"},
        {"貂蝉",19,"女"},
 

#include<iostream>
using namespace std;
#include<string>
 
struct hero
{
	string name;
	int age;
	string sex;
};
 
void BubblSort(hero h[], int len)
{
	for (int i = 0; i < len - 1; i++)
		for (int j = 0; j < len - i - 1;j++)
			if (h[j + 1].age < h[j].age)
			{
				int temp = h[j].age;
				h[j].age = h[j + 1].age;
				h[j + 1].age = temp;
			}
 
}
 
void PrintHero(hero h[], int len)
{
	for (int i = 0; i < len; i++)
		cout << "姓名:" << h[i].name << " 年龄" << h[i].age << " 性别" << h[i].sex << endl;
}
 
int main() {
	hero h[5] =
	{
		{"刘备",23,"男"},
		{"关羽",22,"男"},
		{"张飞",20,"男"},
		{"赵云",21,"男"},
		{"貂蝉",19,"女"},
	};
	BubblSort(h, 5);
	PrintHero(h, 5);

	return 0;
}

感谢您的阅读!欢迎再次访问小涛!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值