专业编程基础技术教程

网站首页 > 基础教程 正文

C++笔记:struct结构体 c++结构体string

ccvgpt 2024-12-30 02:18:58 基础教程 15 ℃

8.1 结构体基本概念

结构体属于用户自定义的数据类型,允许用户存储不同的数据类型

8.2 结构体的定义与使用

struct 结构体名 {结构成员列表};

C++笔记:struct结构体 c++结构体string

  • struct 结构体名 变量名
 #include<iostream>
 using namespace std;
 #include<string>
 //struct 类型名称 {成员列表}
 struct Student
 {
     //成员具体信息
     string name;
     int age;
     int score;
 };
 //通过学生类型创建具体学生
 
 int main()  
 {
     struct Student s1;
     //给s属性赋值,访问结构体变量中的属性
     s1.name = "张三";
     s1.age = 18;
     s1.score = 100;
     cout << "姓名:" << s1.name << endl;
     cout << "年龄:" << s1.age << endl;
     cout << "分数:" << s1.score << endl;
     return 0;
 }
  • struct 结构体名 变量名 = {成员1值,成员2值}
 #include<iostream>
 using namespace std;
 #include<string>
 //struct 类型名称 {成员列表}
 struct Student
 {
     //成员具体信息
     string name;
     int age;
     int score;
 }s1;
 //创建时顺便创建结构体变量
 //通过学生类型创建具体学生
 int main()
 {
     struct Student s1 = { "李四",19,110 };//struct 可省略,
     cout << "姓名:" << s1.name << endl;
     cout << "年龄:" << s1.age << endl;
     cout << "分数:" << s1.score << endl;
     return 0;
 };
  • 定义结构体时顺便创建变量(不推荐)
 #include<iostream>
 using namespace std;
 #include<string>
 //struct 类型名称 {成员列表}
 struct Student
 {
     //成员具体信息
     string name;
     int age;
     int score;
 }s1;
 //创建时顺便创建结构体变量
 //通过学生类型创建具体学生
 int main()
 {
 
     s1.name = "王五";
     s1.age = 20;
     s1.score = 120;
     cout << "姓名:" << s1.name << endl;
     cout << "年龄:" << s1.age << endl;
     cout << "分数:" << s1.score << endl;
     return 0;
 };

用.来访问结构体变量中的值

8.3 结构体数组

将自定义的结构体放入数组中方便维护




 #include<iostream>
 using namespace std;
 struct student
 {
     string name;
     int age;
 };
 int main()
 {
     struct student arr[3] =
     {
         {"张三",18},
         {"李四",19},
         {"王五",20}
     };
     //给其中的元素赋值
     arr[1].age = 20;
     arr[1].name = "赵六";
     //输出结果
     for (int i = 0; i < 3; i++)
     {
         cout << "姓名:" << arr[i].name << " 年龄: " << arr[i].age << endl;
     }
     return 0;
 }



8.4 结构体指针



 #include<iostream>
 using namespace std;
 #include<string>
 struct student
 {
     string name;
     int age;
     int score;
 };
 
 int main()
 {
     struct student s = { "张三",18,100 };
     struct student* p = &s;//通过指针指向结构体变量
     cout << " 姓名 " << p->name 
          << " 年龄 " << p->age 
          << " 分数 " << p->score << endl;
     cout << " 姓名 " << s.name
          << " 年龄 " << s.age
          << " 分数 " << s.score << endl;
     //上面的两个输出语句等价
     return 0;
 }

8.5 结构体嵌套结构体



 #include<iostream>
 using namespace std;
 #include<string>
 struct student
 {
     string name;
     int age;
     int score;
 };  
 struct teacher
 {
     int id;
     string name;
     int age;
     struct student std;//student结构体要先定义
 };
 int main()
 {
     struct teacher t;
     t.id = 1000;
     t.name = "老王";
     t.age = 50;
     t.std.name = "小王";//通过teacher结构体继续控制student结构体
     t.std.age = 18;
     t.std.score = 100;
     cout << "老师姓名:" << t.name << endl
          << "老师编号: " << t.id << endl
          << "老师年龄: " << t.age << endl
          << "老师辅导学生的姓名: " << t.std.name << endl
          << "老师辅导学生的年龄: " << t.std.age << endl
          << "老师辅导学生的分数: " << t.std.score << endl;
     return 0;
 }

也可以在变量t的中括号内依次输入

 #include<iostream>
 using namespace std;
 #include<string>
 struct student
 {
     string name;
     int age;
     int score;
 };  
 struct teacher
 {
     int id;
     string name;
     int age;
     struct student std;//student结构体要先定义
 };
 int main()
 {
     struct teacher t {10,"老李",50,"小李",19,110 };
     //t.id = 1000;
     //t.name = "老王";
     //t.age = 50;
     //t.std.name = "小王";//通过teacher结构体继续控制student结构体
     //t.std.age = 18;
     //t.std.score = 100;
     cout << "老师姓名:" << t.name << endl
          << "老师编号: " << t.id << endl
          << "老师年龄: " << t.age << endl
          << "老师辅导学生的姓名: " << t.std.name << endl
          << "老师辅导学生的年龄: " << t.std.age << endl
          << "老师辅导学生的分数: " << t.std.score << endl;
     return 0;
 }

8.6 结构体做函数参数



值传递

 #include<iostream>
 using namespace std;
 #include<string>
 struct student
 {
     string name;
     int age;
     int score;
 };  
 void p(struct student s)
 {
     cout << "姓名:" << s.name 
         << " 年龄:" << s.age 
         << " 分数:" << s.score << endl;
 }
 int main()
 {
     struct student s = { "张三",18,100 };
     p(s);
     return 0;
 }

地址传递

 #include<iostream>
 using namespace std;
 #include<string>
 struct student
 {
     string name;
     int age;
     int score;
 };  
 void p(struct student* s)
 {
     s->age = 200;
     cout << "姓名:" << s->name 
         << " 年龄:" <<  s->age
         << " 分数:" << s->score << endl;//调用指针用箭头
 }
 int main()
 {
     struct student s = { "张三",18,100 };
     p(&s);
     cout << "main函数中张三的年龄:" << s.age << endl;
     return 0;
 }

地址传递的情况下形参改变会影响实际参数,而值传递不会

8.7 结构体中const的使用场景

目的:用const防止误操作

 #include<iostream>
 using namespace std;
 #include<string>
 struct student
 {
     string name;
     int age;
     int score;
 };  
 void p(const struct  student *s)//在最前面加上const
 {
     s->age = 1;//报错,防止出现误操作
     cout << "姓名:" << s->name
         << " 年龄:" << s->age
         << " 分数:" << s->score << endl;
 }
 int main()
 {
     struct student s = { "张三",18,100 };
     p(&s);//用指针访问可以减少内存的使用
     
     return 0;
 }



Tags:

最近发表
标签列表