c++复杂数据类型——结构体、共用体、枚举、匿名类型、类型别名

结构体

定义
	struct MyStruct
    {
        int code;
        string name;
        float salary;
    } myStruct1;
	MyStruct myStruct2;
	auto myStruct3 = new MyStruct(); //指针
	MyStruct myStruct4 = MyStruct();
初始化
    struct MyStruct
    {
        int code;
        string name;
        float salary;
    } myStruct1 = {20,"bill", 2550};

    MyStruct myStruct2  = {20,"bill", 2550};  //  C语言需要添加struct关键字,C++不需要
    
    auto myStruct3 = new MyStruct();
    myStruct3->name = "ABC";
    
    auto mySturct4 = (MyStruct){20,"bill", 2550};
    mySturct4.code = 200;
    mySturct4.name = "abc";
    mySturct4.salary = 20.3;
    
    MyStruct myStruct5 = MyStruct();
    mySturct5.code = 200;
    mySturct5.name = "abc";
    mySturct5.salary = 20.3;    
    
结构体数组
    struct MyStruct
    {
        int code;
        string name;
        int values[2];
    };
    
    MyStruct myStructArray1[100];
    myStructArray1[0].name = "bill";
    
    MyStruct myStructArray2[2] = {{20, "Mary",{1,2}}, {30, "John", {3,4}}};
    cout << myStructArray2[0].values[1] << endl;
    
    //不使用强制类型转换,auto关键字无法识别类型
    auto myStructArray3 = (MyStruct[]){{20, "Mary",{1,2}}, {30, "John", {3,4}}};
    cout << myStructArray3[1].name << endl;
结构体尺寸

结构体尺寸的计算规则:
在默认情况下,结构体尺寸是结构体中占字节最多的成员变量的尺寸的整数倍
更通用的说法:结构体尺寸是当前对其方式的整数倍

    struct MyStruct1
    {
        int code1;
        int code2;
    };
    
    cout << "MyStruct1 size:" << sizeof(MyStruct1) << endl; //4
    
    struct MyStruct2
    {
        short code1;
        char c;
    } myStruct2{20,'a'};
    
    cout << "MyStruct2 size:" << sizeof(MyStruct2) << endl; //4
结构体对齐方式
    //  结构体的默认对齐方式
    unsigned int *n1 = (unsigned int*)&myStruct2;
    bitset<32> b1(*n1);
    cout << "myStruct2二进制表示:" << b1 << endl;  // 00000000 01100001 00000000 00010100
    /*
     0000000001100001   0000000000010100  short2个字节,char1个字节,均以16位存储,不够16位以0补齐
     20: 0000000000010100
     'a':01100001 0000000001100001
     */
	
	struct MyStruct3
    {
        char c;
        short code1;
    } myStruct3{'a',20};
    
    cout << "MyStruct3 size:" << sizeof(MyStruct3) << endl; // 4
    unsigned int *n2 = (unsigned int*)&myStruct3; 
    bitset<32> b2(*n2);
    cout << "myStruct3二进制表示:" << b2 << endl; // 00000000 00010100 00000000 01100001

	修改结构的对齐方式:
	#pragma pack(1) //宏指令改变对齐方式规则,设置为1以一个字节为对齐方式,结构体尺寸则为数据类型自己的整数倍,MyStruct3的size为3, 二进制表示为: 00000000 00000000 00010100 01100001
	// #pragma pack(2) MyStruct3的size为4
	// #pragma pack(4) MyStruct3的size为4 因为如果结构体成员变量默认的对齐方式与修改后的对齐方式冲突,以最小的为准
空结构体的size
    struct NullStruct
    {
    };
    cout << "NullStruct Size:" << sizeof(NullStruct) << endl; // 1
结构体的位字段
	struct MyStruct1
    {
        unsigned int code1; // 32
        unsigned int code2; // 32
        bool flag; //1就够用
    };
    
	struct MyStruct2
    {
        unsigned int code1:4; // 设置位字段为4
        unsigned int code2:4;
        bool flag:1;
    } myStruct2{17,15,false}; // 4位二进制最大值为1111 = 15,17溢出, 10001取4位 0001 = 1
    cout << sizeof(MyStruct1) << endl; //12
    cout << sizeof(MyStruct2) << endl; // 4, 根据结构体尺寸原则,结构体尺寸是结构体中占字节最多的成员变量的尺寸的整数倍, 可以容纳9位的4的最小整数倍是1,所以size为4
    cout << myStruct2.code2 << endl;
    
    unsigned int *n = (unsigned int*)(&myStruct2);
    bitset<32> b(*n);
    cout << b << endl; // 00000000000000000000000  0  1111  0001

共用体(联合体)

定义,赋值
    union MyUnion1
    {
        int code1;
        long long code2;
        float price;
        bool flag;
    } myUnion;
    struct MyStruct
    {
        int code1; 
        long long code2;
        float price;
        bool flag;
    } myStruct{20,30,10.4,true};
    cout << "MyUnion1 size:" << sizeof(MyUnion1) << endl; // 8
    cout << "MyStruct size:" << sizeof(MyStruct) << endl; // 24 4 + 8 + 4 + 最小为1 = 17 ,临近17的最近的8的倍数为24
    myUnion.price = 10.1;
    cout << "myUnion.price=" << myUnion.price << endl; // myUnion.price=10.1
    myUnion.code2 =100;
    cout << "myUnion.price=" << myUnion.price << endl; // myUnion.price=1.4013e-43, 共用体中后者赋值会覆盖前一个成员的值
位字段,二进制存储格式
    union MyUnion2
    {
        int code1;
        long long code2:4;   // 位数有符号或无符号决定于变量类型是有符号还是无符号
        float price;
        bool flag;
    } myUnion2;
    myUnion2.code2 = 13;
    cout << "myUnion2.code2=" << myUnion2.code2 << endl; // -3
    cout << "MyUnion2 size:" << sizeof(MyUnion2) << endl; // 8
    //  13二进制:1101  取补码: 1011 , 有符号,为-3

枚举

定义,赋值
	// enum类型中值为int类型,后一个值在前一个值基础上+1
    enum color{red /*0*/,yellow /*1*/,green/*2*/};
    color flowerColor;
    flowerColor = green;
	
	// enmu类型定义全局唯一,不能重复定义
	enum color1{red /*0*/,yellow /*1*/,green/*2*/}; // 会提示报错
	
	// 只能后加1,不能向前减1
	enum Color2{RED2/*0*/, GREEN2 =20, BLUE2/*21*/};
	enum Color3{RED3/*0*/, GREEN3 = 0, BLUE3/*1*/,YELLOW3=1 };
	enum Color4{RED4 = -2, GREEN4/*-1*/, BLUE4/*0*/ };
	enum Color5{RED5 = 'A', GREEN5/*66*/, BLUE5 /*67*/};
枚举类

C++ 11新特性

    //  与enum区别:允许相同的枚举成员, 不能直接输出成员变量值,必须强制转换类型
    enum class Color3{RED, GREEN,BLUE = 20};
    enum class Color4{RED, GREEN,BLUE};
    
    Color3 color3 = Color3::GREEN;
    cout << int(Color3::BLUE) << endl;
    cout << color1::green<< endl;
枚举类型尺寸
    enum Color1{RED,GREEN,BLUE};
    cout << "Color1 size=" << sizeof(Color1) << endl; // 4, int类型尺寸为4
    enum class Color2{RED = INT32_MAX-2,GREEN ,BLUE /*= INT32_MAX + 1:溢出,会出错*/};
    cout << "Color2 size=" << sizeof(Color2) << endl; // 4
    
    //  修改枚举类型的尺寸
    enum class Color3:short{RED,GREEN,BLUE = INT16_MAX}; // short为两个字节
    cout << "Color3 size=" << sizeof(Color3) << endl; // 2
    
    enum class Color4:unsigned char{RED,GREEN,BLUE = 255}; // 1 char为1个字节
    cout << "Color4 size=" << sizeof(Color4) << endl;
    
    //  C++ 11
    enum class Color5:int8_t{RED,GREEN,BLUE = -128}; // 1 int8_t为1个字节

匿名类型

	struct
    {
        int code;
        string name;
        int values[2];
    } myStruct;
    

类型别名

	struct
    {
        int code;
        string name;
        int values[2];
    }myStruct;
    union
    {
        int code1;
        long long code2;
        float price;
        bool flag;
    } myUnion;
    enum {red,yellow,green} myEnum;
    // 匿名枚举类中,每一个枚举成员都是全局的,不能和其他枚举类型活匿名枚举类型重名
    enum class {red1,yellow1,green1} myEnumClass;

类型别名

typedef 关键字可将类型变量名变成类型,为复杂的声明定义简单的别名

	typedef struct
    {
        int code;
        string name;
        int values[2];
    }myStruct;
    myStruct st;
    typedef union
    {
        int code1;
        long long code2;
        float price;
        bool flag;
    } myUnion;
    myUnion ui;
    typedef enum {red,yellow,green} myEnum; 
    myEnum eu;
    // 匿名枚举类中,每一个枚举成员都是全局的,不能和其他枚举类型活匿名枚举类型重名
    typedef enum class {red1,yellow1,green1} myEnumClass;
    myEnumClass ec;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值