枚举与数组 [英] Enumeration versus array

查看:507
本文介绍了枚举与数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么会更好:
枚举声明或字符串数​​组:

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

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

string MonthName[12] = {"January", "February", "March", "April", "May", "June", ...)

,因为对我来说,似乎并不像它的问题。

because to me it doesn't seem like it matters.

此变量将仅由一个功能进行访问的基础上,数字输入,显示月

This variable will only be accessed by one function, to display month based on the number input.

推荐答案

如果你想显示的名称当月的,一个枚举会不会给你带来任何好处都没有。在枚举类型定义的唯一标识符的编译器是可见的;你不能在运行时进行打印。

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.

您需要的字符串数组(或者为const char * 组成的数组)。

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

月份的名字也可通过标准库,使用的strftime()在&LT声明功能;的ctime> (从C标准库的<&time.h中GT; 头)。获取月份名称使用的strftime()不是直接给定的月份数(你必须建立一个结构TM 对象在前)。它确实有使用月份名称当前语言环境,而不是硬连接到英语的优势。

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.

和不要忘记,数组的索引从0开始;十二月是在指数11,没有12。

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

这篇关于枚举与数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆