C++基础(04)—— 结构体及typedef

1、结构体和类的区别

结构体属于用户自定义的数据类型,其和类的定义及语法几乎一样。它们唯一的区别在于结构体默认的访问控制属性是公有类型(public),而类的默认访问控制属性是私有类型(private)

2、结构体的定义和使用

(1)、语法: struct   结构体名  {  结构体成员列表 } ;

// 创建Student的结构体
struct Student
{
	string name;
	int age;
	int score;
}s3;    // s3 为该结构体的一个变量。

int main() 
{
	// Student结构体的实例化

	// 实例化方法一:
	Student s1;
	s1.name = "Karl";
	s1.age = 18;
	s1.score = 90;
	cout << "name : " << s1.name << "age : " << s1.age << "score : " << s1.score << endl;

	// 实例化方法二:(当相应成员值缺省时,默认值为0或者空)
	Student s2 = { "Hrllt",20,99 };
	cout << "name : " << s2.name << " , age : " << s2.age << " , score : " << s2.score << endl;
	
	// 实例化方法三:在创建时就实例化s3,s3为该结构体的一个变量。
	
	cin.get();
}

(2)、结构体前有 typedef 时,其后的Stu 为结构体类型的别名。

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

// Stu 为结构体Student 的别名
typedef struct Student 
{
	string name;
	int age;
}Stu;   // Stu 为该结构体类型的别名

typedef struct Student
{
	string name;
	int age;
} *ST;  // 等价于Student类型的指针: Student * 

int main()
{
	Stu s1; //等价于  Student s1;
    ST s2 = &s1; // 等价于 Student* s2=&s1 ;

	cin.get();
}

(3)、结构体数组

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

// 创建Student的结构体
struct Student
{
	string name;
	int age;
	int score;
};

int main() 
{
	// 定义结构体数组的同时就对其进行初始化,只初始化前两个
	Student Array1[3] = 
	{
		{"A01",18,99},
		{"A02",20,100}
	};
	
	// 定义之后再对其进行赋值
	Array1[2].name = "A03";
	Array1[2].age = 22;
	Array1[2].score = 98;

	// 遍历结构体数组
	for (int i = 0; i < 3; i++) 
	{
		cout << Array1[i].name << "," << Array1[i].age << "," << Array1[i].score << endl;
	}

	cin.get();
}

(4)、结构体指针

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

// 创建Student的结构体
struct Student
{
	string name;
	int age;
	int score;
};

int main() 
{
	// 创建结构体变量
	Student s1 = { "A01",18,100 };

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

	// 通过指针来访问结构体变量中的值
	cout << p->name << "," << p->age << "," << p->score << endl;

	cin.get();
}

(5)、结构体嵌套结构体(结构体的成员是结构体)

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

// 创建Student的结构体
struct Student
{
	string name;
	int age;
	int score;
};

// 创建Teacher的结构体
struct Teacher
{
	int id;
	string name;
	int age;
	Student s2; //学生

};

int main() 
{
	// 创建Teacher结构体变量
	Teacher t1;
	t1.id = 1245;
	t1.name = "wang";
	t1.age = 50;
	t1.s2.name = "A01";
	t1.s2.age = 18;
	t1.s2.score = 100;

	cin.get();
}

(6)、结构体作为函数的参数

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

// 创建Student的结构体
struct Student
{
	string name;
	int age;
	int score;
};

// 值传递的方式
void PrintStudent1(Student s) 
{
	cout << s.name << "," << s.age << "," << s.score << endl;
}

// 地址传递的方式
void PrintStudent2(Student *p) 
{
	cout << p->name << "," << p->age << "," << p->score << endl;
}
int main() 
{
	Student s6 = { "A06",20,100 };

	PrintStudent1(s6);

	PrintStudent2(&s6);  //将地址赋给指针形参

	cin.get();
}

(7)、结构体中的 const 使用

由于在调用函数的时候,如果使用的是值传递,它会把对应的数据都拷贝一份。当需要处理的数据很大时,这种方式就会占用大量的内存,增加内存开销。所以常常使用地址传递,使用指针来进行操作。但是使用指针来进行操作会很容易引起对原始数据的误修改。因此使用const关键字来对其进行限制。使其只能读,而不能修改。

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

// 创建Student的结构体
struct Student
{
	string name;
	int age;
	int score;
};

// 用const来对指针变量进行修饰
void PrintStudent2(const Student *p) 
{
	// p->score = 99;此时不能修改结构体变量的值,只能读。
	cout << p->name << "," << p->age << "," << p->score << endl;
}
int main() 
{
	Student s6 = { "A06",20,100 };

	PrintStudent2(&s6);  //将地址赋给指针形参

	cin.get();
}

3、应用实例

(1)、3个老师,每个老师带2个学生。分别将3个老师和每个老师带的学生信息输出:

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

// 创建Student的结构体
struct Student
{
	string sName;
	int score;
};

// 创建Teacher结构体
struct Teacher
{
	string tName;
	Student s[2];  // Student结构体为其成员变量
};

// 为Teacher的数组成员赋值,将数组作为形参传递给函数
void AllocateSpace(Teacher tArray[],int len1) 
{
	string str1 = "ABC";
	for (int i = 0;i<len1;i++) 
	{
		tArray[i].tName = "Teacher_";
		tArray[i].tName += str1[i];
		for (int j = 0; j < 2; j++) 
		{
			tArray[i].s[j].sName = "Student_";
			tArray[i].s[j].sName += str1[j];
			// 将成绩设置成为随机数
			int random = rand() % 61 + 40; //将random的值设定在40到100之间
			tArray[i].s[j].score = random;
		}
		
	}
}

void Print(const Teacher tArray[],int len) 
{
	for (int i = 0; i < len; i++) 
	{
		cout << "教师名字: " << tArray[i].tName << endl;
		for (int j = 0; j < 2; j++) {
			cout << "\t学生姓名:" << tArray[i].s[j].sName <<
				" , " << "学生成绩: " << tArray[i].s[j].score << endl;
		}
	}

}
int main() 
{
	// 设置随机数种子,以产生真正的随机数,保证公平
	srand((unsigned int)time(NULL));

	Teacher tArray[3];
	int len = sizeof(tArray) / sizeof(tArray[0]);
	AllocateSpace(tArray, len);
	Print(tArray, len);

	cin.get();
}

(2)、有5个人物的结构体数组,将5个人物的年龄按照冒泡排序法进行从大到小进行排序,并打印出来最终结果

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

struct TheRole
{
	string name;
	int age;
	string gender;

};

// 冒泡排序法
void bubbleSort(TheRole Array[],int len)
{
	// 外层循环
	for (int i = 0; i < len-1; i++)
	{
		for (int j = 0; j < len - i - 1; j++)
		{
			if (Array[j].age > Array[j + 1].age) {
				TheRole temp = Array[j];
				Array[j] = Array[j + 1];
				Array[j + 1] = temp;
			}
		}
		
	}
}

// 打印结果
void PrintINfo(TheRole *role,int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << "姓名: " << role[i].name << " , " <<
			"年龄: " << role[i].age << " , " <<
			"性别: " << role[i].gender << endl;
	}
}

int main()
{
	TheRole role[5]=
	{
		{"A",23,"男"},
		{"B",22,"男"},
		{"C",20,"男"},
		{"D",21,"男"},
		{"D",19,"女"}
	};

	int len = sizeof(role) / sizeof(role[0]);
	bubbleSort(role, len);
	PrintINfo(role,len);

	cin.get();
}

参考资料:

[1]   struct和typedef struct彻底明白

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值