c++结构体
结构体基本概念
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
结构体定义和使用
语法
struct 结构体名{结构体成员列表}
通过结构体创建变量的方式有三种:
struct 结构体名 变量名;
struct 结构体名 变量名 = {成员1值。成员2值...};
定义结构体时顺便创建变量。
访问结构体变量的属性:
变量名.属性
在c++中,在创建结构体变量时候,struct可以省略,结构体定义时,关键字不能省略。
#include<iostream>
#include<string>
using namespace std;
struct Student
{
string name;
int age;
int score;
string sex;
}s3;
int main()
{
struct Student s1;
s1.name = "念轻佳";
s1.age = 19;
s1.score = 100;
s1.sex = "男";
struct Student s2 = { "念轻佳" ,24 ,100,"女" };
s3.age = 20;
s3.name = "佳";
s3.sex = "女";
s3.score = 120;
cout << " 姓名:" << s1.name << " 年龄:" << s1.age << " 性别:" << s1.sex << " 分数:" << s1.score << endl;
cout << " 姓名:" << s2.name << " 年龄:" << s2.age << " 性别:" << s2.sex << " 分数:" << s2.score << endl;
cout << " 姓名:" << s3.name << " 年龄:" << s3.age << " 性别:" << s3.sex << " 分数:" << s3.score << endl;
system("pause");
return 0;
}
结构体的数组
作用:将自定义的结构体放入到数组中方便维护
语法:
struct 结构体名 数组名[元素个数] = {{} ,{} ,......{} }
#include<iostream>
#include<string>
using namespace std;
struct Personal
{
string name;
int age;
int score;
string sex;
};
int main()
{
//创建结构体数组
struct Personal PerArr[3] =
{
{"念轻佳",28,180,"女"},
{"念轻佳1",26,150,"女"} ,
{"念轻佳",18,150,"男"}
};
for (int i = 0; i < 3; i++)
{
cout << "名字 " << PerArr[i].name
<< " 年龄" << PerArr[i].age
<< " 分数" << PerArr[i].score
<< " 性别" << PerArr[i].sex
<< endl;
}
system("pause");
return 0;
}
结构体指针
作用:通过指针访问结构体中的成员
利用操作符-> 可以通过结构体指针访问结构体属性
#include<iostream>
#include<string>
using namespace std;
//定义结构体
struct Personal
{
string name;
int age;
int score;
string sex;
};
int main()
{
//声明和赋值结构体
struct Personal per = { "念轻佳",28,180,"女" };
//创建结构体指针,对结构体进行操作
struct Personal* p = &per;
p->name = "佳佳";
p->age = 25;
p->score = 100;
cout << "名字:" << p->name
<< "年龄" << p->age
<< "分数" << p->score
<< "性别" << p->sex
<< endl;
system("pause");
return 0;
}
结构体嵌套结构体
作用:结构体中的成员可以是另一个结构体
列如:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体
#include<iostream>
using namespace std;
//定义学生结构体
struct Student
{
string name;
int age;
int score;
};
//定义老师结构体
struct Teacher
{
int id;
string name;
int age;
struct Student st;
};
int main()
{
//两种方式都能赋初值
Teacher t = { 1000 ,"王老师" ,30, "小李" ,18,80};
//t.id = 1000;
//t.age = 30;
//t.name = "王老师";
//t.st.age = 18;
//t.st.name = "小李";
//t.st.score = 80;
cout << "老师姓名: " << t.name
<< " 老师编号:" << t.id
<< " 老师年龄:" << t.age
<< " 学生姓名:" << t.st.name
<< " 学生年龄:" << t.st.age
<< " 学生分数:" << t.st.score
<< endl;
system("pause");
return 0;
}
结构体做函数参数
作用:将结构体作为参数向函数中传递
传递方式有两种:
值传递和地址传递
#include<iostream>
using namespace std;
struct Student
{
string name;
int age;
string sex;
int score;
};
//值传递修改打印,不会修饰main函数的传过来的结构体
void printf1(struct Student s1)
{
s1.age = 100;
cout << "姓名:" << s1.name
<< " 年龄:" << s1.age
<< " 性别:" << s1.sex
<< " 分数:" << s1.score
<< endl;
}
//地址传递会修饰传过来的结构体。
void printf2(struct Student *s1)
{
s1->age = 100;
cout << "姓名:" << s1->name
<< " 年龄:" << s1->age
<< " 性别:" << s1->sex
<< " 分数:" << s1->score
<< endl;
}
int main()
{
Student s1 = { "小张",25,"男",150 };
//printf1(s1);
printf2(&s1);
cout << "姓名:" << s1.name
<< " 年龄:" << s1.age
<< " 性别:" << s1.sex
<< " 分数:" << s1.score
<< endl;
system("pause");
return 0;
}
结构体中const使用场景
作用:用const来防止误操作
#include<iostream>
using namespace std;
struct Student
{
string name;
int age;
string sex;
int score;
};
void printf3(const Student *s)
{
//s->age = 150;
cout << s->name << s->age << s->score << s->sex << endl;
}
int main()
{
struct Student s = { "佳佳",25,"男",180 };
printf3(&s);
system("pause");
return 0;
}
案例:
#include<iostream>
#include<string>
using namespace std;
struct Hero
{
string name;
int age;
string sex;
};
void paop(Hero h[])
{
for (int i = 0; i < 5-1; i++)
{
for (int j = 0; j < 5-i-1; j++)
{
if (h[j].age > h[j + 1].age)
{
Hero t = h[j];
h[j] = h[j + 1];
h[j + 1] = t;
}
}
}
for (int i = 0; i < 5; i++)
{
cout << h[i].name<<h[i].age<<h[i].sex<<"\t";
}
}
int main()
{
Hero hArr[5] = { {"关羽",18,"男"},{"刘备",25,"男"},{"貂蝉",38,"女"},{"张飞",17,"男"},{"赵云",89,"男"} };
paop(hArr);
system("pause");
return 0;
}