How to Get enum item name from its value

后端 未结 10 975
天涯浪人
天涯浪人 2020-12-08 09:56

I declared a enum type as this,

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

How can I get the item na

10条回答
  •  不思量自难忘°
    2020-12-08 10:11

    An enumeration is something of an inverse-array. What I believe you want is this:

    const char * Week[] = { "", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };  // The blank string at the beginning is so that Sunday is 1 instead of 0.
    cout << "Today is " << Week[2] << ", enjoy!";  // Or whatever you'de like to do with it.
    

提交回复
热议问题