C++枚举数据结构
有人可以告诉我对枚举数据结构的实时需求吗?例如在一些真实的系统中可以使用它吗?以及为什么会有这样的数据结构。给出的例子是
enum colors_t {black, blue, green, cyan, red, purple, yellow, white};
但是我觉得,这类似于字符串数组。我试图理解将此功能添加到 C++ 中的概念。谢谢 !
- 问候 塞图
Can someone give me a real time need for enum data structure. As in example in some real system where it can be used? And what is the reason for having such a data structure. The example given was
enum colors_t {black, blue, green, cyan, red, purple, yellow, white};
But i felt, this is similar to string array. I am trying to understand the notion with which this feature was added to C++. Thanks !
- Regards
Sethu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
例如,考虑以下枚举:
这表示 IP 数据包中的 IP 协议号。在数据包内部,协议号由数字而不是名称(字节偏移9处的8位)来标识。此枚举允许程序源引用诸如 icmp 和 tcp 之类的名称,而不是数字。该编号必须在 IP 数据包本身内部使用。
For example, consider this enumeration:
This represents the IP protocol number in IP packets. Inside a packet, the protocol number is identified by a number rather than a name (8 bits at byte offset 9). This enumeration lets the program source refer to names like
icmp
andtcp
rather than numbers. The number must be used inside the IP packet itself.当您使用
enum
时,值(例如black
、green
等)是在内部表示为整数的符号名称。如果您使用字符串数组,那么每次您想要使用其中一个值时,您都需要复制字符串、进行字符串比较等。
此外,
enum
可以只包含预定义的值,这在大多数情况下都是可取的。如果您表示与字符串相同的东西,则没有这样的保证。When you use an
enum
the values (such asblack
,green
, etc.) are symbolic names that are represented internally as integers.If you used a string array, then every time you wanted to use one of those values you’d need to copy the string, do a string compare, etc.
In addition, the
enum
can only contain the pre-defined values, which is desirable most of the time. If you represented the same thing as string there’s no such guarantee.枚举数(如示例中的
black
)由整数表示;比较整数很快。比较字符串并不快。此外,您还可以获得类型安全。如果您使用
"black"
和"blue"
之类的字符串,则无法保证当您输入"hot dogs"
时,不会有人通过您。期待一种颜色。如果您选择color_t
,您一定会获得有效的颜色(除非有人走错了地方)。An enumerator (like
black
in your example) is represented by an integer; comparing integers is fast. Comparing strings is not fast.In addition, you get type safety. If you used strings like
"black"
and"blue"
, there's no guarantee that someone doesn't pass you"hot dog"
when you expect a color. If you take acolor_t
, you are guaranteed to get a valid color (unless someone has gone and done something wrong).根据记录,Bjarne Stroustrup 在《The Design and Exolution of C++》一书中谈到将
enum
引入 C++,并声明“C 枚举构成了一个奇怪的半生不熟的概念。枚举并不是 C 最初概念的一部分,并且显然是不情愿地引入到该语言中的,作为对那些坚持获得比 Cpp 无参数宏更实质性的符号常量形式的人们的让步。” (第 11.7 节,“Cpp”指的是 C 预处理器,本节的其余部分记录了在 C++ 中使每个enum
成为单独类型而不是全部为int
的决定> 与它们最初在 C 和 C++ 的早期版本中的样子一样)。enum
主要是作为替换早期版本的 C 中的#define
的一种
。
方式 att.com/~bs/bs_faq2.html#in-class" rel="nofollow">使用哪个很大程度上取决于个人品味。
enum
确实提供了一种关于变量应保存何种值的文档形式:这在函数签名中特别有用:
switch< 中允许
enum
/code> 语句case
标签(例如#define
和const int
):但是,请注意,它是 可以将数字分配给没有标记值的
enum
(C++0x 添加了一个不允许这样做的enum class
):如果您如果你见过一个带有 2 的幂的显式值的
enum
,你几乎可以保证它会以这种方式使用。For the record, Bjarne Stroustrup talks about bringing
enum
s into C++ in the book The Design and Exolution of C++ with the statement "C enumerations constitute a curiously half-baked concept. Enumerations were not part of the original conception of C and were apparently reluctantly introduced into the language as a concession to people who insisted on getting a form of symbolic constants more substantial than Cpp's parameterless macros." (section 11.7, "Cpp" refers to the C preprocessor, the rest of the section chronicles the decision to make eachenum
a separate type in C++ instead of all of them beingint
s as they originally were in C and early versions of C++).enum
s are largely meant as a way of replacing#define
s in earlier versions of C.vs.
vs.
Which to use is largely a matter of personal taste. An
enum
does provide a form of documentation about what kind of values a variable should hold:This is especially helpful in function signatures:
enum
s are allowed inswitch
statementcase
labels (as are#define
s andconst int
s):However, note that it is possible to assign numbers to
enum
s that don't have a labeled value (C++0x adds anenum class
that doesn't allow this):If you ever see an
enum
with explicit values that are powers of 2, you can almost guarantee that it will be used this way.1)枚举与整数:枚举更容易阅读,因此代码更易于维护。
2)枚举与字符串:枚举更有效(也与整数一样有效)。
附加优势:枚举会自动限制在您预定义的值范围内。这也意味着它们仅在值范围较小时才有用。
1) Enum vs Integers: Enums are easier to read and hence code is more maintenable.
2) Enum vs Strings: Enums are much more efficient (alsmo as effecient as integers).
Added advantage: Enums are automatically restricted to the range of values you predefine. This also means that they are useful only when the range of values is small.