📜  C++中的枚举类及其相对于枚举数据类型的优势

📅  最后修改于: 2021-05-30 11:00:59             🧑  作者: Mango

枚举或枚举类型(枚举)是用户定义的数据类型,可以分配一些有限的值。这些值由程序员在声明枚举类型时定义。

在Enum类型上需要Enum类:
以下是一些关于Enum Type的限制的原因以及为什么我们需要Enum Class来覆盖它们的一些原因。

两个枚举不能共享相同的名称:

CPP
#include 
using namespace std;
 
int main()
{
    // Defining enum1 Gender
    enum Gender { Male,
                  Female };
 
    // Defining enum2 Gender2 with same values
    // This will throw error
    enum Gender2 { Male,
                   Female };
 
    // Creating Gender type variable
    Gender gender = Male;
    Gender2 gender2 = Female;
 
    cout << gender << endl
         << gender2;
 
    return 0;
}


CPP
#include 
using namespace std;
 
int main()
{
    // Defining enum1 Gender
    enum Gender { Male,
                  Female };
 
    // Creating Gender type variable
    Gender gender = Male;
 
    // creating a variable Male
    // this will throw error
    int Male = 10;
 
    cout << gender << endl;
 
    return 0;
}


CPP
#include 
using namespace std;
 
int main()
{
    // Defining enum1 Gender
    enum Gender { Male,
                  Female };
 
    // Defining enum2 Color
    enum Color { Red,
                 Green };
 
    // Creating Gender type variable
    Gender gender = Male;
    Color color = Red;
 
    // Upon comparing gender and color
    // it will return true as both have value 0
    // which should not be the case actually
    if (gender == color)
        cout << "Equal";
 
    return 0;
}


CPP
// C++ program to demonstrate working
// of Enum Classes
 
#include 
using namespace std;
 
int main()
{
 
    enum class Color { Red,
                       Green,
                       Blue };
    enum class Color2 { Red,
                        Black,
                        White };
    enum class People { Good,
                        Bad };
 
    // An enum value can now be used
    // to create variables
    int Green = 10;
 
    // Instantiating the Enum Class
    Color x = Color::Green;
 
    // Comparison now is completely type-safe
    if (x == Color::Red)
        cout << "It's Red\n";
    else
        cout << "It's not Red\n";
 
    People p = People::Good;
 
    if (p == People::Bad)
        cout << "Bad people\n";
    else
        cout << "Good people\n";
 
    // gives an error
    // if(x == p)
    // cout<<"red is equal to good";
 
    // won't work as there is no
    // implicit conversion to int
    // cout<< x;
 
    cout << int(x);
 
    return 0;
}


C++
#include 
using namespace std;
enum rainbow{
    violet,
    indigo,
    blue,
    green,yellow,orange,red
}colors;
enum class eyecolor:char{
    blue,green,brown
}eye;
int main() {
 
    cout<<"size of enum rainbow variable: "<


编译错误:

prog.cpp:13:20: error: redeclaration of 'Male'
     enum Gender2 { Male,
                    ^
prog.cpp:8:19: note: previous declaration 'main()::Gender Male'
     enum Gender { Male,
                   ^
prog.cpp:14:20: error: redeclaration of 'Female'
                    Female };
                    ^
prog.cpp:9:19: note: previous declaration 'main()::Gender Female'
                   Female };
                   ^
prog.cpp:18:23: error: cannot convert 'main()::Gender' 
to 'main()::Gender2' in initialization
     Gender2 gender2 = Female;
                       ^







任何变量都不能具有已经在某些枚举中使用的名称:

CPP

#include 
using namespace std;
 
int main()
{
    // Defining enum1 Gender
    enum Gender { Male,
                  Female };
 
    // Creating Gender type variable
    Gender gender = Male;
 
    // creating a variable Male
    // this will throw error
    int Male = 10;
 
    cout << gender << endl;
 
    return 0;
}

编译错误:

prog.cpp: In function 'int main()':
prog.cpp:16:9: error: 'int Male' redeclared as different kind of symbol
     int Male = 10;
         ^
prog.cpp:8:19: note: previous declaration 'main()::Gender Male'
     enum Gender { Male,
                   ^

枚举不是类型安全的

CPP

#include 
using namespace std;
 
int main()
{
    // Defining enum1 Gender
    enum Gender { Male,
                  Female };
 
    // Defining enum2 Color
    enum Color { Red,
                 Green };
 
    // Creating Gender type variable
    Gender gender = Male;
    Color color = Red;
 
    // Upon comparing gender and color
    // it will return true as both have value 0
    // which should not be the case actually
    if (gender == color)
        cout << "Equal";
 
    return 0;
}

警告:

prog.cpp: In function 'int main()':
prog.cpp:23:19: warning: comparison between 'enum main()::Gender'
and 'enum main()::Color' [-Wenum-compare]
     if (gender == color)                ^

枚举类

C++ 11引入了枚举类(也称为作用域枚举),使枚举既具有强类型又具有强作用域。类枚举不允许隐式转换为int,也不允许比较来自不同枚举的枚举。
要定义枚举类,我们在枚举关键字之后使用class关键字。
句法:

// Declaration
enum class EnumName{ Value1, Value2, ... ValueN};

// Initialisation
EnumName ObjectName = EnumName::Value;

例子:

// Declaration
enum class Color{ Red, Green, Blue};

// Initialisation
Color col = Color::Red;

下面是显示Enum类的实现

CPP

// C++ program to demonstrate working
// of Enum Classes
 
#include 
using namespace std;
 
int main()
{
 
    enum class Color { Red,
                       Green,
                       Blue };
    enum class Color2 { Red,
                        Black,
                        White };
    enum class People { Good,
                        Bad };
 
    // An enum value can now be used
    // to create variables
    int Green = 10;
 
    // Instantiating the Enum Class
    Color x = Color::Green;
 
    // Comparison now is completely type-safe
    if (x == Color::Red)
        cout << "It's Red\n";
    else
        cout << "It's not Red\n";
 
    People p = People::Good;
 
    if (p == People::Bad)
        cout << "Bad people\n";
    else
        cout << "Good people\n";
 
    // gives an error
    // if(x == p)
    // cout<<"red is equal to good";
 
    // won't work as there is no
    // implicit conversion to int
    // cout<< x;
 
    cout << int(x);
 
    return 0;
}
输出
It's not Red
Good people
1

声明为enum类的枚举类型也对其底层类型具有更多控制权;它可以是任何整数数据类型,例如char,short或unsigned int,它们实际上是用来确定类型的大小的。

由枚举类型后面的冒号和基础类型指定:

eg: enum class eyecolor : char {char,green,blue};
Here eyecolor is a distinct type with the same size as a char (1 byte).

C++

#include 
using namespace std;
enum rainbow{
    violet,
    indigo,
    blue,
    green,yellow,orange,red
}colors;
enum class eyecolor:char{
    blue,green,brown
}eye;
int main() {
 
    cout<<"size of enum rainbow variable: "<
输出
size of enum rainbow variable: 4
size of enum class eyecolor variable:1

参考: https://en.cppreference.com/w/cpp/language/enum

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”