Javer 学 c++(九):结构体篇

总览

主要讲了 c++ 中结构体的定义、嵌套使用以及和函数、const 的结合使用

定义和使用

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

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

通过结构体创建变量的方式有三种,见下代码:

c++ 复制代码
#include <iostream>
using namespace std;

struct Student {
    string name;
    int age;
    int score;
}s3;

int main() {

    // 创建方式一:创建空的然后赋值
    struct Student s1;
    s1.name = "John";
    s1.age = 18;
    s1.score = 10;
    cout << s1.name << " " << s1.age << " " << s1.score << endl;
    // 创建方式二
    struct  Student s2 = {"River", 18, 100 };
    // 创建方式三:定义结构体的时候顺手的事
    s3.name = "Jewel";
    s3.age = 18;
    s3.score = 10;

}

一种 Java 没有的数据类型,Java 里万物皆类

  • 创建结构体变量的时候,struct 关键字可以省略
  • 结构体通过 . 来访问成员

结构体指针

通过指针->来访问结构体的成员

c++ 复制代码
#include <iostream>
using namespace std;

struct Student {
    string name;
    int age;
    int score;
}s3;

int main() {

    Student s = {"River", 20, 100};
    Student * p = &s;
    // 使用 -> 箭头来访问
    cout << p->name << " " << p->age << " " << p->score << endl;

}

结构体嵌套结构体

结构体的成员可以是另一个结构体

c++ 复制代码
#include <iostream>
using namespace std;

struct Student {
    string name;
    int age;
    int score;
};

struct Teacher {
    int id;
    string name;
    int age;
    Student student;
};

int main() {

    Teacher t;
    t.id = 2;
    t.name = "John";
    t.age = 10;

    t.student = {"River", 10, 20};

    return 0;
}

结构体做函数参数

将结构体作为参数向函数中传递: 值传递 or 引用传递

c++ 复制代码
#include <iostream>
using namespace std;

struct Student {
    string name;
    int age;
    int score;
};

// 值传递
void printStudent1(Student student) {
    student.age = 10; // 值传递:原来的对象值不变
    cout << student.name << student.age << student.score << endl;
}

// 地址传递
void printStudent2(Student * p) {
    p->age = 10; // 地址传递:原来的对象值会变
    cout << p->name << p->age << p->score << endl;
}

int main() {

    Student student = {"River", 10, 20};

    printStudent1(student);

    Student * p = &student;
    printStudent2(p);

    return 0;
}

结构体中 const 的使用

作用:用 const 来防止误操作

c++ 复制代码
#include <iostream>
using namespace std;

struct Student {
    string name;
    int age;
    int score;
};

// 地址传递
void printStudent2(const Student * p) {
    p->age = 10; // 报错,因为是 const,解决了引用误操作的问题
    cout << p->name << p->age << p->score << endl;
}

int main() {

    Student student = {"River", 10, 20};

    Student * p = &student;
    printStudent2(p);

    return 0;
}

值传递每次都要复制一份,在结构体较大以及调用次数较多的时候,会非常占用空间;而引用传递每次只需要 8 字节,可以很好的解决这个问题

但是引用传递有一个隐患就是会修改实参,所以有时候需要加个 const 来防止误修改

相关推荐
神奇的程序员3 小时前
从已损坏的备份中拯救数据
运维·后端·前端工程化
oden4 小时前
AI服务商切换太麻烦?一个AI Gateway搞定监控、缓存和故障转移(成本降40%)
后端·openai·api
李慕婉学姐5 小时前
【开题答辩过程】以《基于Android的出租车运行监测系统设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·后端·vue
m0_740043735 小时前
SpringBoot05-配置文件-热加载/日志框架slf4j/接口文档工具Swagger/Knife4j
java·spring boot·后端·log4j
kk哥88996 小时前
C++ 对象 核心介绍
java·jvm·c++
helloworddm6 小时前
WinUI3 主线程不要执行耗时操作的原因
c++
招风的黑耳6 小时前
我用SpringBoot撸了一个智慧水务监控平台
java·spring boot·后端
Miss_Chenzr6 小时前
Springboot优卖电商系统s7zmj(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
期待のcode6 小时前
Springboot核心构建插件
java·spring boot·后端
2501_921649496 小时前
如何获取美股实时行情:Python 量化交易指南
开发语言·后端·python·websocket·金融