目录
1.结构体属于用户自定义的数据类型,允许用户储存不同的数据类型
2.定义结构体&声明结构体
3.结构体数组
4.结构体指针
5.结构体嵌套结构体
6.结构体作为函数参数
7.结构体应用
1.结构体属于用户自定义的数据类型,允许用户储存不同的数据类型
2.定义结构体&声明结构体
语法:struct 结构体名 {结构体成员列表};
#include <iostream>
#include <string>
using namespace std;
struct Student //一些类型的集合
{
string name;
int age;
int score;
} s3; //s3为定义时同时创建一个student类型的结构体
//创建方式
//1. struct Student s1;
//2. struct Student s2 ={....};
//3.在结构体最后顺便定义
int main()
{
//方法一
struct Student s1;//struct在创建时关键字struct可以省略
//给S1成员赋值
s1.name = "张三";
s1.age = 10;
s1.score = 100;
//方法二
struct Student s2 = {"李四", 12, 99}; //按照结构体中的定义顺序给成员赋值
cout << s1.name << " 年龄" << s1.age << " 成绩" << s1.score << endl;
cout << s2.name << " 年龄" << s2.age << " 成绩" << s2.score << endl;
system("pause");
return 0;
}
3.结构体数组
语法:
struct 结构体名 数组名【元素个数】 ={? ?{... ,...? },{},{}? };
#include <iostream>
#include <string>
using namespace std;
struct Student //一些类型的集合
{
string name;
int age;
int score;
} ;
int main()
{
//创建结构体数组
struct Student s1[3]=
{
{"张三",3,120},
{"李四",3,120},
{"张三",3,120}
};
s1[2].name ="HSL";
s1[2].age =88;
cout << s1[0].name << " 年龄" << s1[0].age << " 成绩" << s1[0].score << endl;
cout << s1[2].name << " 年龄" << s1[2].age << " 成绩" << s1[2].score << endl;
system("pause");
return 0;
}
4.结构体指针
为结构体创建指针,数据类型为结构体类型
语法: strcut 结构体名 *指针名 =&结构体变量;
注意当指针指向结构体数组时,不用&和->,而是直接使用 .变量名
结构体中含有数组时,方法不变,在变量名后加【元素序号】
#include <iostream>
#include <string>
using namespace std;
struct Student //一些类型的集合
{
string name;
int age;
int score[2];
};
int main()
{
//创建结构体变量(数组)
struct Student s1[3] =
{
{"张三", 3, {120,100}},
{"李四", 3, {120,32}},
{"", 3, {120,35}}
};
s1[2].name = "HSL";
s1[2].age = 88;
struct Student s2 = {"lio", 10, {110,20}};
//创建指针指向结构体变量(数组)
struct Student *p = s1; //数组型结构体不用加&
// 使用指针指向结构体数组不用->,实质:数组名本质是一种指针
cout << p[2].name << " 年龄" << p[2].age << " 成绩" << p[2].score[0] << endl;
p = &s2; //指向普通结构体变量
//
cout << p->name << " 年龄" << p->age << " 成绩" << p->score[1] << endl;
system("pause");
return 0;
}
5.结构体嵌套结构体
#include <iostream>
#include <string>
using namespace std;
struct Student //一些类型的集合
{
string name;
int age;
int score;
};
//结构体嵌套
struct teacher
{
int id;
struct Student stu;
};
int main()
{
struct teacher t;
t.id = 10;
t.stu.name = " wang ";
t.stu.age = 10;
t.stu.score = 100;
cout << t.id << " name" << t.stu.name << " 成绩" << t.stu.score << endl;
system("pause");
return 0;
}
6.结构体作为函数参数
1.值传递
2.地址传递
3.结构体数组的地址传递
#include <iostream>
#include <string>
using namespace std;
struct Student //一些类型的集合
{
string name;
int age;
int score;
} s1, s2 ;
//值传递
void printmesg1(struct Student s)
{
cout << s.name << "年龄" << s.age << " 成绩" << s.score << endl;
}
//地址传递
void printmesg2(struct Student *p)
{
cout << p->name << "年龄" << p->age << " 成绩" << p->score << endl;
p->name = "t";
}
//结构体数组的地址传递
void printmesg3(const struct Student *p)//加入const可以保护元数据不被误修改
{
cout << p[0].name << "年龄" << p[0].age << " 成绩" << p[0].score << endl;
}
int main()
{
s1 = {"hsl", 20, 100};
s2 = {"hr", 18, 98};
Student s3[2] = {{"thr", 22, 100}, {"hsl", 22, 120}};
printmesg1(s1);
printmesg2(&s2);
printmesg3(s3);
cout << s2.name << "年龄" << s2.age << " 成绩" << s2.score << endl;//地址传递改变值,实参也会变
system("pause");
return 0;
}
7.结构体应用
要求:
一个老师有三个学生,使用结果结构体打印出三个老师的姓名和他们学生的姓名,成绩
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
struct Student //一些类型的集合
{
string name;
int score;
};
struct teacher
{
string name;
Student arrstu[3];
};
void allocateSpace(struct teacher *arr, int len)//arr[]=*arr
{
string nameSeed = "ABCDE";
for (int i = 0; i < len; i++)
{
arr[i].name = nameSeed[i];
for (int j = 0; j < 3; j++)
{
arr[i].arrstu[j].name = nameSeed[j];
arr[i].arrstu[j].score = rand()%61+40;
}
}
}
void printInfo(struct teacher arr[], int len)
{
for (int i = 0; i < len; i++)
{
cout << "老师:" << arr[i].name << endl;
for (int j = 0; j < 3; j++)
{
cout << "\t学生:"
<< arr[i].arrstu[j].name
<< "分数:" << arr[i].arrstu[j].score << endl;
}
}
}
int main()
{
// 随机数种子
srand((unsigned int)time(NULL));
struct teacher tArray[3];
int len = sizeof(tArray) / sizeof(tArray[0]);
allocateSpace(tArray, len);
printInfo(tArray, len);
system("pause");
return 0;
}
要求:
对学生成绩进行降序排列,顺序打印。
#include <ctime>
#include <iostream>
#include <string>
using namespace std;
struct Student //一些类型的集合
{
string name;
int age;
int score;
};
void printInfo(struct Student arr[], int len)
{
for (int i = 0; i < len; i++)
{
cout << "name:" << arr[i].name << " age: " << arr[i].age << " score: " << arr[i].score << endl;
}
}
void bubbleSort(struct Student *arr, int len)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - 1 - i; j++)
{
struct Student temp;
if (arr[j].score < arr[j + 1].score)
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main()
{
// 随机数种子
Student s1[5] =
{{"张飞", 38, 15}, {"留被", 54, 59}, {"关于", 22, 98}, {"貂蝉", 18, 75}, {"沙雕", 100, 0}
};
int len = sizeof(s1) / sizeof(s1[0]);
bubbleSort(s1, len);
printInfo(s1, len);
system("pause");
return 0;
}
|