c++ 语法结构体

语法

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

通过结构体创建变量

struct 结构体名 变量名

struct 结构体名 变量名 = {成员1值,成员2值...}

定义结构体时顺便创建变量

#include 
#include 
using namespace std;
struct Student{
    string name;
    int age;
    int score;
}stu3;

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    //struct 结构体名 变量名 struct 关键字可以省略
    struct Student stu1;
    stu1.name = "李四";
    stu1.age = 18;
    stu1.score = 98;
    
    //struct 结构体名 变量名 = {成员1值,成员2值...}
    struct Student stu = {"张三",18,100};
    
    stu3.name = "王五";
    stu3.age = 60;
    stu3.score = 100;
    
    return 0;
}

定义结构体 struct 关键字不能省略

创建结构体变量 struct 可以省略

访问成员变量使用点语法

结构体数组

struct 结构体名 数组名[元素个数] = {{},{},{}...{}}

#include 
#include "mathutil.hpp"
#include 
using namespace std;
struct Student{
    string name;
    int age;
    int score;
}stu3;
int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    struct Student stus[] = {{"张三",18,100},{"李四",18,100},{"王五",18,100}};
    stus[0].age = 20;
    for (int i = 0; i < 3; i++) {
        std::cout << "姓名:" << stus[i].name
        <<"年龄:" << stus[i].age << "分数" << stus[i].score << std::endl;
    }
    
    return 0;
}

结构体指针

#include 
#include "mathutil.hpp"
#include 
using namespace std;
struct Student{
    string name;
    int age;
    int score;
}stu3;
int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    Student s1 = Student{"王帅",20,98};
    Student *stu = &s1;
    std::cout << stu->name << stu->age << stu->score << std::endl;
    
    return 0;
}

使用->访问成员属性

结构体嵌套

#include 
#include "mathutil.hpp"
#include 
using namespace std;
struct Student{
    string name;
    int age;
    int score;
}stu3;
struct Teacher{
    int id;
    int age;
    string name;
    Student stu[3];
};

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    Teacher teacher;
    teacher.id = 11204028;
    teacher.age = 35;
    teacher.name = "李名";
    teacher.stu[0] = {"张三",18,100};
    teacher.stu[1] = {"李四",18,100};
    teacher.stu[2] = {"王五",18,100};
    std::cout << teacher.name << teacher.age << teacher.id << std::endl;
    for (int i = 0; i < 3; i++) {
        std::cout << "姓名:" << teacher.stu[i].name
        <<"年龄:" << teacher.stu[i].age << "分数" << teacher.stu[i].score << std::endl;
    }
    return 0;
}

结构体作为参数

#include 
#include "mathutil.hpp"
#include 
using namespace std;
struct Student{
    string name;
    int age;
    int score;
}stu3;
/// 值传递
void studentInfo(Student stu) {
    std::cout << "姓名:" << stu.name
    <<"年龄:" << stu.age << "分数" << stu.score << std::endl;
}
//地址传递
void studentInfo2(Student *stu) {
    stu->name = "王武";
}

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    Student s1 = Student{"王帅",20,98};
    studentInfo(s1);
    studentInfo2(&s1);
    std::cout << "姓名:" << s1.name
    <<"年龄:" << s1.age << "分数" << s1.score << std::endl;
    return 0;
}

结构体使用const

#include 
#include "mathutil.hpp"
#include 
using namespace std;
struct Student{
    string name;
    int age;
    int score;
}stu3;
void studentInfo(const Student *stu) { /// + const 防止函数中修改变量
    std::cout << "姓名:" << stu->name
    <<"年龄:" << stu->age << "分数" << stu->score << std::endl;
}


int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    Student s1 = Student{"王帅",20,98};
    studentInfo(s1);
    return 0;
}

demo 练习

//
//  main.cpp
//  cpplearn
//
//  Created by KING on 2024/2/1.
//

#include 
#include "mathutil.hpp"
#include 
using namespace std;
struct Student{
    string name;
    int age;
    int score;
}stu3;
struct Teacher{
    int id;
    int age;
    string name;
    Student stu[3];
};


void initData(struct Teacher teachers[], int len){
    string  sufix = "ABC";
    for (int i = 0; i < len; i++) {
//        teachers[i].name = "teacher_";
//        teachers[i].name = teachers[i].name + sufix[i];
//        teachers[i].age = 32 + i;
//        teachers[i].id = 11204028 + i;
        Teacher teacher;
        teacher.name = "teacher_";
        teacher.name = teacher.name + sufix[i];
        teacher.age = 32 + i;
        teacher.id = 11204028 + i;
        teachers[i] = teacher;
        int count = sizeof( teachers[i].stu) / sizeof(teachers[i].stu[0]);
        for (int j = 0; j < count; j++) {
            Student stu;
            stu.name = "stu_";
            stu.name = stu.name + sufix[j];
            stu.age = 18 + j;
            stu.score = rand() % 60 + 40;
            teacher.stu[j] = stu;
            
//            teachers[i].stu[j].name =  teachers[i].name + "stu_" + sufix[j];
//            teachers[i].stu[j].age = 18 + j;
//            teachers[i].stu[j].score = rand() % 60 + 40;

        }
    }
}
int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    struct Teacher teachers[3];
    int count = sizeof(teachers) / sizeof(teachers[0]);
    initData(teachers,count);
    for (int i = 0; i < count; i++) {
        Teacher teacher = teachers[i];
        std::cout << "teacher"  << "id=" << teacher.id << "name = " << teacher.name << "age" << teacher.age << std::endl;
        int count = sizeof(teacher.stu) / sizeof(teacher.stu[0]);
        for (int j = 0; j < count; j++) {
            Student stu = teacher.stu[j];
            std::cout << "\t stu" << "name=" << stu.name << "age=" << stu.age << "score=" <<  stu.score << std::endl;
        }
    }
    return 0;
}

你可能感兴趣的:(c++,开发语言)