概念
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型?
语法
struct 结构体名 { 结构体成员列表 };?
通过结构体创建变量的方式有三种:struct 关键字可以去掉
struct 结构体名 变量名 struct 结构体名 变量名 = { 成员1值 , 成员2值…} 定义结构体时顺便创建变量
#include <iostream>
using namespace std;
// 定义结构体
struct student{
string name;
int age;
string hobby[3];
}stu3; // 实例化结构体方式三 定义结构体时实例化
int main(){
// 实例化结构体方式一:
struct student stu1 = student{"haha", 20, {"python", "golang", "c++"}};
cout << "姓名:" << stu1.name << " 年龄:" << stu1.age << " 爱好:" << stu1.hobby[0] << "," << stu1.hobby[1] << "," << stu1.hobby[2] << endl;
// 实例化结构体方式二:
struct student stu2;
stu2.name = "hehe";
stu2.age = 19;
stu2.hobby[0] = "c++";
cout << "姓名:" << stu2.name << " 年龄:" << stu2.age << " 爱好:" << stu2.hobby[0] << endl;
stu3.name = "heihei";
stu3.age = 18;
stu3.hobby[0] = "python";
stu3.hobby[1] = "c++";
cout << "姓名:" << stu3.name << " 年龄:" << stu3.age << " 爱好:" << stu3.hobby[0] << "," << stu3.hobby[1] << endl;
cout << sizeof(stu1) << endl; // 136
cout << sizeof(stu2) << endl; // 136
cout << sizeof(stu3) << endl; // 136
cout << sizeof(struct student) << endl; // 136
}
总结1:定义结构体时的关键字是struct,不可省略
总结2:结构体定义完成后,所占与的字节数就已经确定
总结3:结构体通过 ' . '?访问成员,或为成员变量赋值
结构体数组
作用:将自定义的结构体放入到数组中方便维护
语法:struct 结构体名 数组名[元素个数] = { {} , {} , ... {} }
#include <iostream>
using namespace std;
// 定义结构体
struct student{
string name;
int age;
string hobby[3];
}
int main(){
// 结构体定义完成后,所占与的字节数就已经确定
cout << sizeof(struct student) << endl;
// 表示数组arr内的元素类型是 student 结构体
student arr[3] = {
{"haha", 20, {"python", "golang", "c++"}},
{"hehe", 19, {"c++"}},
{"heihei", 18, {"python", "c++"}},
};
for (int i = 0; i < sizeof(arr)/ sizeof(student); ++i) {
cout << "姓名: " << arr[i].name << endl;
}
}
结构体指针
- 利用操作符?
->? 可以通过结构体指针访问结构体属性
#include <iostream>
using namespace std;
// 定义结构体
struct student{
string name;
int age;
string hobby[3];
};
int main(){
struct student stu1 = student{"haha", 20, {"python", "golang", "c++"}};
// 结构体指针
struct student * p = &stu1;
cout << p->hobby << endl; // 0x7ffd0edc1458
cout << *p->hobby << endl; // python 解释: *p->hobby 等价于 *(p->hobby) 等价于 p->hobby[0] 等价于 stu1.hobby[0]
cout << p->hobby[0] << endl; // python 等价于 stu1.hobby[0]
cout << p->name << endl; // haha 等价于 stu1.name
cout << p->age << endl; // 20 等价于 stu1.age
}
结构体嵌套
结构体中的成员可以是另一个结构体
#include <iostream>
using namespace std;
struct student{
string name;
int age;
string hobby[3];
};
struct teacher{
int id;
string name;
struct student stu;
};
int main(){
// 实例化结构体方式一:
struct student stu1 = student{"haha", 20, {"python", "golang", "c++"}};
// 结构体嵌套
struct teacher tea;
tea.id = 1;
tea.name = "wang";
tea.stu = stu1;
cout << "teacher id = " << tea.id
<< " teacher name= " << tea.name
<< " student name= " << tea.stu.name
<< " student age= " << tea.stu.age << endl;
}
结构体做函数参数
将结构体作为参数向函数中传递
传递方式有两种:
#include <iostream>
using namespace std;
// 定义结构体
struct student{
string name;
int age;
string hobby[3];
};
// 值传递
void printStudent(student stu){
stu.age = 22;
cout << "值传递中打印:" << endl;
cout << "姓名:" << stu.name
<< " 年龄:" << stu.age
<< " 爱好:" << stu.hobby[0] << "," << stu.hobby[1] << "," << stu.hobby[2] << endl;
}
// 地址传递
void printStudent2(student * stu){
stu->age = 25;
cout << "地址递中打印:" << endl;
cout << "姓名:" << stu->name
<< " 年龄:" << stu->age
<< " 爱好:" << stu->hobby[0] << "," << stu->hobby[1] << "," << stu->hobby[2] << endl;
}
int main(){
// 实例化结构体方式一:
struct student stu1 = student{"haha", 20, {"python", "golang", "c++"}};
cout << "初始值:" << endl;
cout << "姓名:" << stu1.name
<< " 年龄:" << stu1.age
<< " 爱好:" << stu1.hobby[0] << "," << stu1.hobby[1] << "," << stu1.hobby[2] << endl;
printStudent(stu1);
cout << "值传递后打印:" << endl;
cout << "姓名:" << stu1.name
<< " 年龄:" << stu1.age
<< " 爱好:" << stu1.hobby[0] << "," << stu1.hobby[1] << "," << stu1.hobby[2] << endl;
printStudent2(&stu1);
cout << "地址递后打印:" << endl;
cout << "姓名:" << stu1.name
<< " 年龄:" << stu1.age
<< " 爱好:" << stu1.hobby[0] << "," << stu1.hobby[1] << "," << stu1.hobby[2] << endl;
}
>>>>:
初始值:
姓名:haha 年龄:20 爱好:python,golang,c++
值传递中打印:
姓名:haha 年龄:22 爱好:python,golang,c++
值传递后打印:
姓名:haha 年龄:20 爱好:python,golang,c++
地址递中打印:
姓名:haha 年龄:25 爱好:python,golang,c++
地址递后打印:
姓名:haha 年龄:25 爱好:python,golang,c++
结构体中 const使用场景
作用:用const来防止误操作
#include <iostream>
using namespace std;
// 定义结构体
struct student{
string name;
int age;
string hobby[3];
};
// 地址传递, 使用地址传递的意义是为了节省内存
void printStudent2(const student * stu){ // const 修饰了指针 -- 变量指针
stu->age = 25; // 报错 指针指向的值不可以修改
}
int main(){
struct student stu1 = student{"haha", 20, {"python", "golang", "c++"}};
printStudent2(&stu1);
}
|