• C++入门教程(七、结构体)


    前面教程汇总

    第一讲

    C++入门教程(一、初步了解)

    第二讲

    C++入门教程(二、基本数据类型)

    第三讲

    C++入门教程(三、数组、注释与函数)

    第四讲

    C++入门教程(四、运算符与表达式)

    第五讲

    C++入门教程(五、数组与字符串)

    第六讲

    C++入门教程(六、指针)

    结构体

    什么是结构体?

    你可以把它想象成一个桌面上的文件夹,这个文件夹里面可以有各种各样的文件,当然也还可以再有文件夹的存在,文件夹里面再放文件……如果你要修改其中一个文件的内容,就是首先通过桌面上的那个文件夹作为入口,然后一个一个的进入文件夹去寻找你需要的文件,找到之后就可以随你修改了。

    longunsigned intshortchar(相当于各种文件类型,比如 .txt、.c、.h)这些关键字是否很熟悉?这都是 C 语言和C++定义好的数据类型,直接拿来用就行了。但是我想自定义一个别的类型的数据怎么办?

    就靠 struct 了。结构体,顾名思义,就是将一个个数据类型构成一个数据类型以方便使用。

    结构体是一种自定义的数据类型,是创建变量的模板,不占用内存空间,真正需要开辟内存空间来存储的是结构体成员变量,结构体中的各个成员理论上在内存中都是连续存储的,但实际上是根据字节对齐规则进行存储的。

    结构体的定义

    这是结构体定义的一个基本框架:

    struct StructName
    {
    	//在这里你可以定义任何的变量和函数,比如:
    	int a, b, c;
    	string s; //需要引入头文件string
    	void foo(int x)
    	{
    		cout << x * 2 << endl;
    	}
    }; //在}和;之间可以写上想用这个数据类型定义的变量或数组,也可以空着,在后面再加StructName 变量
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    结构体的使用

    结构体的使用是以“变量名.数据类型内元素”或“数组元素名.数据类型内元素”。我们举一个例子:

    #include 
    using namespace std;
    
    struct foo
    {
    	int a = 8;
    	void func(int x)
    	{
    		cout << x * 8 / 17 << endl;
    	}
    }t;
    
    foo f[1010];
    
    int main()
    {
    	cout << t.a << endl;
    	t.func(17);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    懂了吧?结构体就是这样运作的。

    结构体指针

    当一个指针变量指向结构体时,称为结构体指针。

    struct 结构体名 *变量名;
    
    • 1

    结构体变量名与数组名不一样,数组名在表达式中会被转换成数组指针,而结构体变量名不会,无论在任何表达式中标表示的都是整个结构体本身,要活的结构体变量的地址,则必须使用取地址符 &

    结构体指针获取结构体成员

    通过结构体指针获取结构体成员的方式:

    (*ptr).structMember
    ptr->structMember
    
    • 1
    • 2

    . 运算符高于 * ,所以 (*ptr) 括号不能少。

    我举个例子,看看你能否看懂:

    #include 
    using namespace std;
    
    struct test
    {
        int num;
        char ch;
        double doub;
    }t1;
    
    int main()
    {
        struct test t1 = {5,'A',5.6};
        struct test *ptr = &t1;
        //解引用和点运算符
        printf("type1: (*ptr).num=%d\n",(*ptr).num);
        //指针
        printf("type2:  ptr->ch=%c\n",ptr->ch);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    结构体指针作为函数参数

    结构体变量名代表整个结构体变量本身,当把结构体变量本身作为参数传递时,结构体内部的结构体成员较多时会造成时间和空间开销大,影响程序的运行效率。

    使用结构体指针作为参数传递时,因为传递的是地址,会比原本含有较多结构体的结构体变量更加块,但是也会造成结构体变量数据被修改的可能。

    再举个例子:

    #include 
    #include 
    using namespace std;
    
    struct test
    {
        int num;
        char ch;
        double doub;
    }t1;
    
    void printA(struct test str)
    {
        str.num += 5;
        printf("printA函数的num=%d\n",str.num);
    }
    
    void printB(struct test* te)
    {
        te->num += 15;
        printf("printB函数的num=%d\n",te->num);
    }
    
    int main()
    {
        clock_t start, finish, start1, finish1;
        struct test t1 = {5,'A',5.6};
        struct test *ptr = &t1;
        start = clock();
        printA(t1);
        finish = clock();
        double Total_time = (double)(finish - start);
        printf("printA函数之后,main函数的num=%d\n",t1.num);
        printf("printA运行时间=%f\n",Total_time);
        start1 = clock();
        printB(ptr);
        finish1 = clock();
        double Total_time1 = (double)(finish1 - start1);
        printf("printB函数之后,main函数的num=%d\n",t1.num);
        printf("printB运行时间=%f\n",Total_time1);
        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++入门教程(七、结构体)”,你学会了吗?

  • 相关阅读:
    腾讯二面:如何保证接口幂等性?高并发下的接口幂等性如何实现?
    Jmeter接口测试 —— jmeter对图片验证码的处理
    软件测试基础 - 测试覆盖率
    OPNsense 配置LDAP Authentication on Active Directory
    0000-0002 UVa227 Puzzles UVa232 Crossword Answers UVA1368 DNA Consensus String
    Redis中常见数据结构和数据类型
    QFeamework中的MVC
    腾讯云4核8G服务器性能如何多少钱一年?
    Python进阶——Socket编程
    实时云渲染技术在虚拟仿真领域的应用
  • 原文地址:https://blog.csdn.net/weixin_59197425/article/details/126061454