结构体入门
本入门教程需要有基本的代码阅读能力
结构体语法及其成员变量赋值方式
定义和使用结构体,以及如何给结构体的成员变量赋值。
#include <iostream>
using namespace std;
/*
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
语法:
struct 结构体名{
成员变量;
};
*/
struct Student
{
string name;
int age;
int score;
}s3; //在定义结构体时,在花括号后顺便直接创建结构体变量
int main()
{
//方式1,通过.访问结构体变量中的属性
Student s1;
s1.name = "张三";
s1.age = 24;
s1.score = 99;
cout << "s1.name=" << s1.name << endl;
cout << "s1.age=" << s1.age << endl;
cout << "s1.score=" << s1.score << endl;
//方式2.struct 结构体名 成员名={成员变量1,成员变量2,成员变量3,...}
Student s2 = { "李四",24,98 };
cout << "s2.name=" << s2.name << endl;
cout << "s2.age=" << s2.age << endl;
cout << "s2.score=" << s2.score << endl;
//方式3,在定义结构体时,在花括号后顺便直接创建结构体变量
s3.name = "王五";
s3.age = 24;
s3.score = 97;
cout << "s3.name=" << s3.name << endl;
cout << "s3.age=" << s3.age << endl;
cout << "s3.score=" << s3.score << endl;
system("pause");
return 0;
}
结构体数组
定义和使用结构体数组。
#include <iostream>
using namespace std;
/*
结构体数组
语法:
struct 结构体名 数组名[]={}
*/
struct character {
string name;
string skill;
};
int main()
{
//结构体数组
character heroArray[] =
{
{"海波东","玄冰龙翔"},
{"萧炎","佛怒火莲"}
};
for (int i = 0; i < size_t(sizeof(heroArray) / sizeof(heroArray[0])); i++)
{
cout << heroArray[i].name << " " << heroArray[i].skill << endl;
}
system("pause");
return 0;
}
定义结构体数组:定义了一个名为character的结构体,包含两个字符串类型的成员变量name和skill。然后创建了一个character类型的数组heroArray,并初始化了两个元素。
遍历结构体数组:使用一个for循环遍历数组,并打印每个元素的成员变量。
结构体指针
使用结构体指针来访问结构体的成员。
#include <iostream>
using namespace std;
/*
结构体指针:通过指针访问结构中的成员
*/
struct student
{
string name;
int age;
int score;
};
int main()
{
student s = { "张三",18,88 };
student* p = &s;
cout << "s姓名" << p->name << endl;
cout << "s年龄" << p->age << endl;
cout << "s分数" << p->score << endl;
system("pause");
return 0;
}
创建结构体实例:定义并初始化了一个student类型的结构体实例s。
创建结构体指针:创建了一个指向student类型的指针p,并将其指向s。
通过指针访问成员:使用箭头->操作符通过指针访问结构体的成员变量。
结构体嵌套
结构体嵌套的概念,即一个结构体可以作为另一个结构体的成员。
#include <iostream>
using namespace std;
/*
结构体嵌套和结构体
被嵌套的结构体应该在被哦嵌套之前被定义
*/
struct student {
string name;
int age;
};
struct teacher
{
string name;
int age;
student s1;
};
int main()
{
teacher t1 = { "老王",60 };
t1.s1 = { "张三",23 };
cout << t1.name << "辅导的学生是" << t1.s1.name << endl;
system("pause");
return 0;
}
定义嵌套结构体:首先定义了一个student结构体,然后定义了一个teacher结构体,其中包含一个student类型的成员变量s1。
初始化嵌套结构体:创建了teacher类型的实例t1,并初始化了其成员变量name和age。然后,初始化了teacher结构体中的student成员s1。
结构体做函数参数
将结构体作为函数参数传递。
#include <iostream>
using namespace std;
/*
结构体做函数参数:将结构体作为参数想函数中传递
值传递
地址传递
*/
struct student {
string name;
int age;
};
//值传递,不修改实参
void printStudentInfo1(student s) {
s.age = 24;
cout << s.name << " " << s.age;
}
//地址传递,会同步修改实参
void printStudentInfo2(student* p) {//指针只占4个字节,可以减少内存空间
p->age = 24;
cout << p->name << " " << p->age;
}
int main()
{
student s1 = { "张三",23 };
printStudentInfo1(s1);
cout << "值传递后:" << s1.age << endl;
printStudentInfo2(&s1);
cout << "地址传递后:" << s1.age << endl;
system("pause");
return 0;
}
定义结构体:定义了一个student结构体。
值传递函数:定义了一个函数printStudentInfo1,它接受一个student类型的参数。在这个函数中,对参数的修改不会影响原始的实例,因为传递的是值的副本。
地址传递函数:定义了一个函数printStudentInfo2,它接受一个指向student类型的指针。在这个函数中,对指针所指向的实例的修改将反映在原始实例上,因为传递的是地址。