Enumeration versus array

前端 未结 5 1782
滥情空心
滥情空心 2021-01-16 05:57

I was wondering what would be better: An enumeration declaration or a string array:

enum MonthName{January, February, March, April, May, June, ...)


        
相关标签:
5条回答
  • 2021-01-16 06:26

    There is nothing in common between enum and array. With the enum you cannot print the name of the month, this is just an integer...

    In your case you should use

    static const char* MonthName[2] = {"January", "February", ...};
    
    0 讨论(0)
  • 2021-01-16 06:26

    Both enum and strings are different in this case and will produce different results. In case of enum you can store month number with month name in a variable of enum type e.g.

    MonthName mn = March;
    

    The variable mn will carry integer value 2. In case of array of strings with month names you need to specify month number as array index and what you'll get is the string name of the month rather the month number e.g.

    string mn = MonthName[2];
    

    The variable mn will carry the string "March" and can be used to display month. However, you can use both enum and string array in a better way e.g.

    string mn = MonthName[March];
    

    Here, enum March will act as index of the string array MonthName and returns the "March".

    0 讨论(0)
  • 2021-01-16 06:28

    It matters a great deal. If you need printable text, use the array of strings. Enumerations don't translate into readable names; they're just integral values.

    0 讨论(0)
  • 2021-01-16 06:35

    I recommend combining the two, using a table of structs:

    enum Months
    {
        JANUARY = 1,
        FEBRUARY,
        APRIL,
    //...
        DECEMBER,
        NUMBER_OF_MONTHS
    };
    
    struct Month_Enum_Conversion_Entry
    {
        Months                month_id;
        char const * const    month_name;
    };
    
    Month_Enum_Conversion_Entry month_conversion_table[] =
    {
        {JANUARY, "January"},
        {FEBRUARY, "February"},
    //...
        {DECEMBER, "December"}
    };
    static const unsigned int NUM_TABLE_ENTRIES =
        sizeof(month_conversion_table) / sizeof(month_conversion_table[0]);
    

    To convert an enum to text, search the table for the enum and access the text field:

    std::string Month_ID_To_Name(Months m)
    {
        char const * p_month_name = NULL;
        for (unsigned int i = 0; i < NUM_TABLE_ENTRIES; ++i)
        {
            if (month_conversion_table[i].month_id == m)
            {
                p_month_name = month_conversion_table[i].month_name;
                break;
            }
        }
        return std::string(p_month_name);
    }
    

    Two optimizations:

    1. Load table into a std::map for faster conversion.
    2. Add a null, unused entry in the conversion table before January and then index into the table using the month number. The above method is more robust than indexing into an array, as the array has an implied relationship between index and value. The above example provides an explicit relationship between ID and name.
    0 讨论(0)
  • 2021-01-16 06:40

    If you want to display the name of the month, an enum won't do you any good at all. The identifiers in an enumeration type definition are visible only to the compiler; you can't print them at run time.

    You need the array of strings (or perhaps an array of const char*s).

    The names of the months are also available via the standard library, using the strftime() function declared in <ctime> (from the C standard library's <time.h> header). Getting the month name for a given month number using strftime() is not straightforward (you'd have to build a struct tm object first). It does have the advantage of using month names for the current locale rather than being hardwired to English.

    And don't forget that array indices start at 0; December is at index 11, not 12.

    0 讨论(0)
提交回复
热议问题