简体   繁体   English

如何从其索引中打印枚举值?

[英]How to print the enum value from its index?

I'm very new to CPP programming.我对 CPP 编程很陌生。 I have a enum:我有一个枚举:

emun foo
{
    Apple,
    Banana,
    Orange
}

For example foo::Banana is printing 1 , but if I have 1 , how to print Banana to the output?例如foo::Banana正在打印1 ,但如果我有1 ,如何将Banana打印到 output? It may be a very silly question.这可能是一个非常愚蠢的问题。

The enum constants names are not known after your program has been compiled.编译程序后, enum常量名称是未知的。 To get the names, you can create an array so you can do the reverse lookup.要获取名称,您可以创建一个数组,以便进行反向查找。 Example:例子:

enum foo {
    Apple,
    Banana,
    Orange
};

const char* revfoo[]{ "Apple", "Banana", "Orange" };

std::cout << revfoo[Apple] << '\n'; // Prints "Apple"

Note: This works for enums starting at 0 and has no gaps.注意:这适用于从 0 开始且没有间隙的枚举。 If your enums are not zero-based or has gaps, you could use an unordered_map instead.如果您的枚举不是从零开始或有间隙,您可以使用unordered_map代替。

#include <string>
#include <unordered_map>

std::unordered_map<foo, std::string> revfoo {
    {Apple, "Apple"},
    {Banana, "Banana"},
    {Orange, "Orange"}
};

std::cout << revfoo[Apple] << '\n'; // Prints "Apple"
#include <iostream>
#include <string>

std::string number_to_String(foo number)
{
    switch(number)
    {
    case Apple:
        return "Apple";
    case Banana:
        return "Banana";
    case Orange:
        return "Orange";
    default:
        return "No data Found";
    }  
}

int main() {
    std::cout << number_to_String(Apple);
    return 0;
}

You can do this way if you want.如果你愿意,你可以这样做。

You can cast your int to an enum, but you must be careful that the int is a valid enum value:您可以将 int 转换为枚举,但必须注意 int 是有效的枚举值:

#include <iostream>

const int value_to_convert = 0; // For example
const foo converted_val = static_cast<foo>(value_to_convert);
std::cout << "Value: " << converted_val << std::endl;

The above print out of the value relies on an overloaded output stream operator:上面的输出值依赖于重载的 output stream 运算符:

#include <ostream>

std::ostream &operator<<(std::ostream &os, const foo &f)
{
    switch (f)
    {
        case foo::Apple:
            os << "Apple";
            break;
        // etc...
    }

    return os;
}

Assuming your enum is zero indexed, you can append an element at the end to denote the number of elements, and use this to check whether the integer is valid before doing a cast.假设您的枚举是零索引的,您可以在 append 末尾添加一个元素来表示元素的数量,并在进行强制转换之前使用它来检查 integer 是否有效。 However, this pollutes the enum and (in my opinion) is slightly messy:但是,这会污染枚举并且(在我看来)有点混乱:

#include <optional>

enum foo
{
    Apple,
    Banana,
    Orange,
    NUM_ELEMENTS
}

std::optional<foo> convert_foo(const int val)
{
    if ((val < 0) || (val >= NUM_ELEMENTS))
    {
        return std::nullopt;
    }
    else
    {
        return static_cast<foo>(val);
    }
}

Note that the use of optional requires C++17 or later.请注意,使用选件需要 C++17 或更高版本。

As you can see, unfortunately C++ doesn't currently offer a nice, clear way of achieving what you desire yet.如您所见,不幸的是 C++ 目前还没有提供一种很好、清晰的方式来实现您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM