结构体的基本概念:
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
语法: struct 结构体名 {结构体成员列表};
通过结构体创建变量的三种方法:
#include<iostream> #include<string> using namespace std; // 定义结构体 struct Student { String name; int age; int score; }; // }s3; //方法三 int main() { // 方法一 struct Student s1; // 创建结构体变量时,关键字struct可省略 // 给s1属性赋值,通过. 访问结构体变量中的属性 s1.name = "张三"; s1.age = 18; s1.score = 100; cout << "姓名" << s1.name << endl; cout << "年龄" << s1.age << endl; cout << "总分" << s1.score << endl; // 方法二 struct Student s2 = {"李四",19,80}; cout << "姓名" << s2.name << endl; cout << "年龄" << s2.age << endl; cout << "总分" << s2.score << endl; // 方法三,在创建结构体后的花括号外创建s3,再同方法一赋值 s3.name = "王五"; s3.age = 20; s3.score = 60; cout << "姓名" << s3.name << endl; cout << "年龄" << s3.age << endl; cout << "总分" << s3.score << endl; system("pause"); return 0; }
用于将自定义的结构体放入但数组中方便维护
语法:struct 结构体名 数组名[元素个数] = { {},{},{}…}
#include<iostream> #include<string> using namespace std; // 结构体定义 struct Student { string name; int age; int score; }; int main() { // 结构体数组 struct student arr[3] = { {"张三",18,80}, {"李四",19,60}, {"王五",20,60} }; // 给结构体数组中的元素赋值 arr[2].name = "赵六"; // 遍历结构体数组 for (int i = 0; i < 3; i++) { cout << "姓名:" << arr[i].name << endl; cout << "年龄:" << arr[i].age << endl; cout << "分数:" << arr[i].score << endl; } system("pause"); return 0; }
作用:通过指针访问结构体中的成员
#include<iostream> #include<string> using namespace std; struct Student { string name; int age; int score; }; int main() { student s = {"张三",18,100}; // 通过指针指向结构体变量 student* p = &s; // 通过指针访问结构体属性 cout << "姓名:" << p->name << endl; cout << "年龄:" << p->age << endl; cout << "分数:" << p->score << endl; system("pause"); return 0; }
结构体中的成员可以是另一个结构体
#include<iostream> #include<string> using namespace std; struct Student { string name; int age; int score; }; struct Teacher { int id; string name; int age; Student stu; }; int main() { // 创建老师及其学生 Teacher t; t.id = 100000; t.name = "老王"; t.age = 50; t.stu.name = "张三"; t.stu.age = 20; t.stu.score = 60; system("pause"); return 0; }
将结构体作为参数向函数中传递
传递方式:
#include<iostream> #include<string> using namespace std; struct Student { string name; int age; int score; }; // 方法一,一般用于不需要修改实参的情形 void printStudent(Student s) { cout << "print name:" << s.name << endl; cout << "print age:" << s.age << endl; cout << "print score:" << s.score << endl; } // 方法二,一般用于需要修改实参的情形 void printStudent2(Student* p) { cout << "p->name:" << p->name << endl; cout << "p->age:" << p->age << endl; cout << "p-score:" << p->score << endl; } int main() { Student s; s.name = "张三"; s.age = 18; s.score = 90; printStudent(s); printStudent2(&s); system("pause"); return 0; }
用const来防止误操作
#include<iostream> #include<string> using namespace std; struct Student { string name; int age; int score; }; // 使用地址传递可以节省内存空间(值传递会再复制一份实参过来) // 使用const可防止函数中结构体的属性值被修改 void printStudent(const Student* p) { // p->name = "李四"; // 报错 cout << "p->name" << p->name << endl; cout << "p->age" << p->age << endl; cout << "p-score" << p->score << endl; } int main() { Student s = {"张三",18,95}; printStudent(&s); system("pause"); return 0; }