枚举数据类型
enum
关键字有什么用?我们可以在哪里使用枚举数据类型? 谁能用例子解释一下吗?
What is the use of the enum
keyword ? Where can we use enumerated data-type?
Can anyone explain with an example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
enum
为数字提供名称。从表面上看,
#define
也是如此:enum
相对于#define
的好处是名称会记录在调试信息中,因此调试器可以使用它。您还可以使用
枚举
来定义一组相关值:然后您可以使用这些值来索引 RGB 值的三元组数组:
可能不是世界上最好的示例,但可以指示可以使用
enum
。An
enum
gives a name to a number.Superficially, so does a
#define
:The benefit of an
enum
over a#define
is that the name is recorded in the debugging information, so the debugger can use it.You can also use an
enum
to define a set of related values:You might then use these to index into a triplet array for the RGB values:
Probably not the world's best example, but an indication of where an
enum
can be used.枚举是始终被视为整数的枚举器(好吧,很简单),它们用于排列稍后可以使用的内容,例如通用值或索引。但是,正如名称所示,它们是枚举的,总是升序,例如:
enum myenum {
一月、二月、三月、四月、五月、六月、七月、八月、九月、十月、十一月、十二月
您
然后,如果您有一个数组,请说:
可以使用以下方式调用您的月份:
另外,如果您声明:
这您的程序将从 1 开始计数,依此类推。
最后,关于最后一条规则:您可以根据需要设置初始值,例如 10、20、30、42 等,但您也可以设置其中一个枚举值,并让其他值默认排序。
Enums are enumerators (ok, trivial) treated always as integers, and they are used to put in order something you could use later, like common values or indexes. But, as the name says, they are enumerated, always ascending, like:
enum myenum {
January, February, March, April, May, June, July, August, September, October, November, December
};
Then, if you have an array, say:
You can call your months using:
Also, if you declare:
This your program will start the counter of the enumeration from 1 and so on.
Finally, about this last rule: you can set initial values as you want, e.g., 10, 20, 30, 42 etc., but you can also set one of the enumerated values and let others be sorted by default.
枚举是在“ANSI C”(或c89)时引入的,基本语法是
它从0开始取值,item1将保存0,item2=1,item3=2..直到你告诉编译器它应该是什么值从开始,
那么值是,item1=10,item2=11,item=12..编译器也会将它们视为整数。
。
您还可以定义枚举类型的变量
在这种情况下,变量 item1 被转换(静默)为类型“enum type”。您可以将“枚举类型”类型的变量与整数混合使用。
尽管变量 mytype 的类型为“枚举类型”,但它也被视为整数,因此它可以保存任何整数变量。
但在我看来,枚举应该用作常量,不应该参与算术运算,并让编译器自行决定值而不是赋值。
enums are introduced at the time of "ANSI C" (or c89) the basic syntax is
It takes value starting from 0, item1 will hold 0, item2=1, item3=2.. Until if you tell the compiler what value it should start from,
then the values are, item1=10, item2=11, item=12.. Also the compiler will consider them as integers.
which is
You can also define a variable of the type enum.
In this case the variable item1 is converted (silently) to the type "enum type". You can mix the variable of type "enum type" with integer.
though the variable mytype is of type "enum type" its also considered as integer hence it can hold any integer variable.
But IMO, enums should be used as constants and should not be involved in arithmetic operation and leave the compiler to decided the value on itself rather than assigning a value.
枚举是常量整数值的列表,如
枚举中的第一个名称的值为 0,下一个为 1,依此类推,除非指定了显式值。如果未指定所有值,则未指定的值将从最后指定的值继续进行:
由于
char
在内部使用整数表示,因此您也可以声明指向字符常量的枚举:
#define
上的枚举可以为您生成值。此外,调试器能够以符号形式打印枚举变量的值。An enumeration is a list of constant integer values, as in
The first name in an enum has value 0, the next 1, and so on, unless explicit values are specified. If not all values are specified, unspecified values continue the progression from the last specified value:
As a
char
is internally represented using an integer, you can declare enumerations that point to character constants as well:An advantage of enumeration over
#define
is that the values can be generated for you. In addition, a debugger may be able to print values of enumeration variables in their symbolic form.