结构体

  1. 结构体的定义

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    #include<iostream>
    #include<string>
    using namespace std;
    struct student //结构体定义
    {
    string name;
    int age;
    }std2,std3; //std2,std3相当于提前声明的结构体变量,这是可选的,可以不声明,也可以声明一个或多个
    int main()
    {
    struct student std1 = { "张三",18 }; //初始化赋值
    cout << std1.name << endl;
    std2.name = "李四"; // 结构体用.去赋值或取值
    std2.age = 19;
    std3.name = "王五";
    std3.age = 20;
    cout << std2.name << endl;
    cout << std3.name << endl;
    return 0;
    }
  2. 结构体数组

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    #include<iostream>
    #include<string>
    using namespace std;
    struct student
    {
    string name;
    int age;
    };
    int main()
    {
    struct student stds[] = {
    {"张三", 18},
    {"李四", 19},
    {"王五", 20}
    };
    cout << stds[0].name << endl;
    return 0;
    }
  3. 结构体指针

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    #include<iostream>
    #include<string>
    using namespace std;
    struct student
    {
    string name;
    int age;
    };
    int main()
    {
    struct student stds = { "张三", 18 };
    struct student* p = &stds;
    cout << p << endl;
    cout << p->name << endl; //对于结构体指针,通过->去取值
    return 0;
    }
  4. 结构体嵌套结构体

    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
    //学生结构体定义
    struct student
    {
    //成员列表
    string name; //姓名
    int age; //年龄
    int score; //分数
    };

    //教师结构体定义
    struct teacher
    {
    //成员列表
    int id; //职工编号
    string name; //教师姓名
    int age; //教师年龄
    struct student stu; //子结构体 学生
    };


    int main() {

    struct teacher t1;
    t1.id = 10000;
    t1.name = "老王";
    t1.age = 40;

    t1.stu.name = "张三";
    t1.stu.age = 18;
    t1.stu.score = 100;

    cout << "教师 职工编号: " << t1.id << " 姓名: " << t1.name << " 年龄: " << t1.age << endl;

    cout << "辅导学员 姓名: " << t1.stu.name << " 年龄:" << t1.stu.age << " 考试分数: " << t1.stu.score << endl;

    system("pause");

    return 0;
    }
  5. 结构体做函数传参

    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
    //学生结构体定义
    struct student
    {
    //成员列表
    string name; //姓名
    int age; //年龄
    int score; //分数
    };

    //值传递
    void printStudent(student stu )
    {
    stu.age = 28;
    cout << "子函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;
    }

    //地址传递
    void printStudent2(student *stu)
    {
    stu->age = 28;
    cout << "子函数中 姓名:" << stu->name << " 年龄: " << stu->age << " 分数:" << stu->score << endl;
    }

    int main() {

    student stu = { "张三",18,100};
    //值传递
    printStudent(stu);
    cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;

    cout << endl;

    //地址传递
    printStudent2(&stu);
    cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;

    system("pause");

    return 0;
    }
  6. 结构体中const使用场景

    可以用const来防止误操作;

    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
    //学生结构体定义
    struct student
    {
    //成员列表
    string name; //姓名
    int age; //年龄
    int score; //分数
    };

    //const使用场景
    void printStudent(const student *stu) //加const防止函数体中的误操作
    {
    //stu->age = 100; //操作失败,因为加了const修饰
    cout << "姓名:" << stu->name << " 年龄:" << stu->age << " 分数:" << stu->score << endl;

    }

    int main() {

    student stu = { "张三",18,100 };

    printStudent(&stu);

    system("pause");

    return 0;
    }