c++ 语法结构体

本文详细介绍了C++中的结构体,包括如何定义结构体、创建变量、使用结构体数组、指针、嵌套结构以及值传递和地址传递。还探讨了结构体成员的访问和const修饰的用法,通过实际代码示例进行说明。
摘要由CSDN通过智能技术生成

语法

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

通过结构体创建变量

struct 结构体名 变量名

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

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

#include <iostream>
#include <string>
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 <iostream>
#include "mathutil.hpp"
#include <string>
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 <iostream>
#include "mathutil.hpp"
#include <string>
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 <iostream>
#include "mathutil.hpp"
#include <string>
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 <iostream>
#include "mathutil.hpp"
#include <string>
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 <iostream>
#include "mathutil.hpp"
#include <string>
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 <iostream>
#include "mathutil.hpp"
#include <string>
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;
}

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值