zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C++中的struct、union与enum

C++ struct union Enum
2023-09-11 14:19:29 时间

0.概述

在本文中,我们将讨论结构体、联合和枚举及其区别。

1.struct

  • struct是C++中可用的用户自定义数据类型。

  • struct用于组合不同类型的数据类型,就像数组用于组合相同类型的数据类型一样。

  • 使用关键字 struct ”声明结构。当我们声明结构变量时,我们需要在 C 语言中编写关键字“ struct,但对于 C++来说,关键字struct不是强制性的。

struct

{

// Declaration of the struct

}

下面是演示struct使用的C++代码.

// C++ program to demonstrate the
// making of structure
#include <bits/stdc++.h>
using namespace std;

// Define structure
struct GFG {
    int G1;
    char G2;
    float G3;
};

// Driver Code
int main()
{
    // Declaring a Structure
    struct GFG Geek;
    Geek.G1 = 85;
    Geek.G2 = 'G';
    Geek.G3 = 989.45;
    cout << "The value is : "
        << Geek.G1 << endl;
    cout << "The value is : "
        << Geek.G2 << endl;
    cout << "The value is : "
        << Geek.G3 << endl;

    return 0;
}

输出结果如下

The value is : 85
The value is : G
The value is : 989.45

说明:在上面的代码中,值被赋值给结构体employee的(G1,G2,G3)字段,最后打印出“salary”的值。

使用 typedef 的结构 typedef是一个关键字,用于为任何现有数据类型分配新名称。下面是 C++ 程序说明使用typedef来使用 struct :

// C++ program to demonstrate the use
// of struct using typedef
#include <bits/stdc++.h>
using namespace std;

// Declaration of typedef
typedef struct GeekForGeeks {

    int G1;
    char G2;
    float G3;

} GFG;

// Driver Code
int main()
{
    GFG Geek;
    Geek.G1 = 85;
    Geek.G2 = 'G';
    Geek.G3 = 989.45;

    cout << "The value is : "
        << Geek.G1 << endl;

    cout << "The value is : "
        << Geek.G2 << endl;

    cout << "The value is : "
        << Geek.G3 << endl;

    return 0;
}

Output:

The value is : 85
The value is : G
The value is : 989.45
  • 在上面的代码中,关键字“ typedef ”被用在了struct之前,structure的右括号之后,写上了“ GFG ”。

  • 现在创建结构变量而不使用关键字“ struct ”和结构名称。

  • 一个名为“Geek”的结构实例已经创建,只需在它前面写上“ GFG ”。

2. union

union 联合是一种结构,可以在使用的内存空间是关键因素,也就是节省内存空间的情况下使用。

  • 与结构类似,联合可以包含不同类型的数据类型。

  • 每次从联合初始化一个新变量时,它会覆盖 C 语言中的前一个变量,但在 C++ 中,我们也不需要此关键字并使用该内存位置。

  • 当通过函数传递的数据类型未知时,这是最有用的,使用包含所有可能数据类型的联合可以解决这个问题。

  • 它是通过使用关键字“ union ”来声明的。

下面是说明 union 实现的 C++ 程序:

// C++ program to illustrate the use
// of the unions
#include <iostream>
using namespace std;

// Defining a Union
union GFG {
    int Geek1;
    char Geek2;
    float Geek3;
};

// Driver Code
int main()
{
    // Initializing Union
    union GFG G1;

    G1.Geek1 = 34;

    // Printing values
    cout << "The first value at "
        << "the allocated memory : " << G1.Geek1 << endl;

    G1.Geek2 = 34;

    cout << "The next value stored "
        << "after removing the "
        << "previous value : " << G1.Geek2 << endl;

    G1.Geek3 = 34.34;

    cout << "The Final value value "
        << "at the same allocated "
        << "memory space : " << G1.Geek3 << endl;
    return 0;
}

Output:

The first value at the allocated memory : 34
The next value stored after removing the previous value : "
The Final value value at the same allocated memory space : 34.34

解释:在上面的代码中,Geek2 变量被赋值了一个整数(34)。但是由于是 char 类型,该值通过强制转换为它的 char 等价物 (")。此结果正确显示在“输出”部分中。

3. enum

enumeratation枚举,枚举是用户定义的类型,由命名的整数常量组成。

  • 它有助于将常量分配给一组名称,使程序更易于阅读、维护和理解。

  • 枚举是通过使用关键字“ enum ”来声明的。

  • 枚举的默认值从0开始,依次递增1,也可以自定义其它值,然后依次递增1,或完整自定义其它值。

下面是说明枚举用法的 C++ 程序:

// C++ program to illustrate the use
// of the Enums

#include <bits/stdc++.h>
using namespace std;

// Defining an enum
enum GeeksforGeeks { Geek1=2,
                    Geek2,
                    Geek3 };

GeeksforGeeks G1 = Geek1;
GeeksforGeeks G2 = Geek2;
GeeksforGeeks G3 = Geek3;

// Driver Code
int main()
{
    cout << "The numerical value "
        << "assigned to Geek1 : "
        << G1 << endl;

    cout << "The numerical value "
        << "assigned to Geek2 : "
        << G2 << endl;

    cout << "The numerical value "
        << "assigned to Geek3 : "
        << G3 << endl;

    return 0;
}

输出

The numerical value assigned to Geek1 : 2
The numerical value assigned to Geek2 : 3
The numerical value assigned to Geek3 : 4

说明:在上面的代码中,Geek1、Geek2、Geek3等命名常量在给定输出时分别赋值为2、3、4等整数。