• C++结构体定义 & 创建 & 赋值 & 结构体数组


    一.结构体是什么?

    struct是自定义数据类型,是一些类型集合组成的一个类型。
    
    • 1

    二.结构体的定义方式

    #include
    using namespace std;
    
    struct Student
    {
    	string name;
    	int age;
    	int score;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    三.创建结构体变量并赋值

    方式一,先创建结构体变量,再赋值

    // 创建结构体变量 , 此时可以省略struce关键字
    struct Student s1;
    // 给结构体变量赋值
    s1.name = "zhangsan";
    s1.age = 26;
    s1.score = 100;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    方式二,创建结构体变量的同时赋值

    struct Student s2={"zhangsan",26,100};
    
    • 1

    方式三,创建结构体的时候顺便创建结构体变量

    struct Student
    {
    	string name;
    	int age;
    	int score;
    }s3;
    
    s3.name = "zhangsan";
    s3.age = 26;
    s3.score = 80;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    四.结构体数组的定义和访问

    #include
    #include
    
    using namespace std;
    
    struct Student
    {
        string name;
        int age;
        int score;
    };
    
    int main()
    {
    
        struct Student stu_arr[3] = 
        {
            {"张三",18,80},
            {"李四",19,60},
            {"王五",38,66}
        };
    
        // 给结构体的元素赋值
        stu_arr[2].name = "赵六";
        stu_arr[2].age = 20;
        stu_arr[2].score = 98;
    
        // 遍历结构体数组
        for(int i=0;i<3;i++)
        {
            cout<<"姓名:"<<stu_arr[i].name<<"年龄: "<<stu_arr[i].age<<"分数: "<<stu_arr[i].score<<endl;
        }
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    编译和执行
    在这里插入图片描述

    五.结构体指针

    #include
    #include
    
    using namespace std;
    
    struct Student
    {
        string name;
        int age;
        int score;
    };
    
    int main()
    {
        struct Student s = {"张三",18,100};
        
        // 创建Student类型的指针
        Student* p = &s;
    
        // 用结构体类型的指针访问结构体的变量
        cout<<"姓名:"<<p->name<<"年龄: "<<p->age<<"分数: "<<p->score<<endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    六.结构体嵌套结构体

    #include
    #include
    
    using namespace std;
    
    struct Student
    {
        string name;
        int age;
        int score;
    };
    
    struct Teacher
    {
        int id;
        string name;
        int age;
        struct Student stu; // 要辅导的学生
    };
    
    int main()
    {
        Teacher t;
        t.id = 1000;
        t.name = "老王";
        t.age = 50;
        // 把学生的值也设置一下
        t.stu.name = "小王";
        t.stu.age = 26;
        t.stu.score = 60;
    
        cout<<t.stu.name<<" "<<t.stu.age<<" "<<t.stu.score<<endl;
    
        return 0;
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    在这里插入图片描述

    七.结构体做函数参数

    #include
    #include
    
    using namespace std;
    
    struct Student
    {
        string name;
        int age;
        int score;
    };
    
    void printStudent1(struct Student s)
    {
        cout<<"子函数中 姓名:"<<s.name<<"年龄: "<<s.age<<"分数: "<<s.score<<endl;
    }
    
    void printStudent2(struct Student* p)
    {
        cout<<"子函数中 姓名:"<<p->name<<"年龄: "<<p->age<<"分数: "<<p->score<<endl;
    }
    
    int main()
    {
        struct Student s = {"张三",18,100};
        
        // 创建Student类型的指针
        Student* p = &s;
    
        printStudent1(s);
        printStudent2(p);
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    在这里插入图片描述

    八.结构体中的const使用场景

    当函数中有结构体形参时:

    会把主函数的student赋值到下面函数的形参,
    如果主函数中的student是个数组,那么也会将数组完全的复制一份给形参。
    在函数中修改形参的值,不会影响主函数中该变量的值。
    
    • 1
    • 2
    • 3

    如果用指针传参:

    优点:用指针传参,一个指针只占四个字节,可以减少内存空间。不会复制新的副本出来。
    缺点:在函数中修改结构体指针,会改变原来的值。
    
    • 1
    • 2

    为了防止在函数中误操作而修改结构体变量的值,传参的时候结构体遍历加 const即可。

    例子:

    #include
    #include
    
    using namespace std;
    
    struct Student
    {
        string name;
        int age;
        int score;
    };
    
    
    // 会把主函数的student赋值到下面函数的形参,如果主函数中的student是个数组,那么也会将数组完全的复制一份给形参。
    void printStudent1(struct Student s)
    {
        s.age = 150;
        cout<<"子函数中 姓名:"<<s.name<<"年龄: "<<s.age<<"分数: "<<s.score<<endl;
    }
    
    // 用指针传参,一个指针只占四个字节,可以减少内存空间。不会复制新的副本出来。
    void printStudent2(const Student* p)
    {
        // 加入const后,一有修改的操作就会报错,防止误操作
        p->age=150;
        cout<<"子函数中 姓名:"<<p->name<<"年龄: "<<p->age<<"分数: "<<p->score<<endl;
    }
    
    int main()
    {
        struct Student s = {"张三",18,100};
        
        // 创建Student类型的指针
        Student* p = &s;
    
        printStudent2(p);
    
        cout<<"main中 姓名:"<<p->name<<"年龄: "<<p->age<<"分数: "<<p->score<<endl;
    
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    补充

    C++输出string类型必须引入 #include

    # 使用输入命令输出报错
    cout<<"姓名"<<s1.name<<"年龄"<<s1.age<<endl;
    
    • 1
    • 2
    因为s1.name是string类型,C++输出string类型必须引入 #include<string>
    
    • 1
  • 相关阅读:
    依赖:类之间的依赖关系【python】
    利用445 端口渗透
    基于SSM的文化培训学校网站的设计与实现
    1.关于433MHz按键单片机解码
    设计模式之发布订阅、观察者模式
    C++搜索二叉树
    双-(二苯胺基-苯基)-苯并[c]硫代咪唑聚集诱导发光微球/四苯基乙烯聚集诱导发光AIE微球
    Flutter基础 -- Flutter布局练习(小项目)
    git 命令总结
    Python 绘制数据图表
  • 原文地址:https://blog.csdn.net/qq_42864343/article/details/134337102