如何从其值中获取枚举项名称

2021-12-29 00:00:00 enums c++

我声明了一个枚举类型,

I declared a enum type as this,

enum WeekEnum
{
Mon = 0;
Tue = 1;
Wed = 2;
Thu = 3;
Fri = 4;
Sat = 5;
Sun = 6;
};

当我已经拥有项目值0、1 等"时,如何获取项目名称周一、周二等"

How can I get the item name "Mon, Tue, etc" when I already have the item value "0, 1, etc."

我已经有了这样的功能

Log(今天是2",享受!);

现在我想要下面的输出

今天是星期三,尽情享受

Today is Wed, enjoy

推荐答案

你不能直接,C++中的enum不像Java枚举.

You can't directly, enum in C++ are not like Java enums.

通常的方法是创建一个std::map.

The usual approach is to create a std::map<WeekEnum,std::string>.

std::map<WeekEnum,std::string> m;
m[Mon] = "Monday";
//...
m[Sun] = "Sunday";

相关文章