简体   繁体   English

C ++枚举数据结构

[英]C++ enum data structure

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++. 我试图了解将此功能添加到C ++的概念。 Thanks ! 谢谢 !

  • Regards Sethu 问候塞图

For example, consider this enumeration: 例如,考虑以下枚举:

enum ip_packet_type {
    ip = 0,
    icmp = 1,
    igmp = 2,
    tcp = 6,
    udp = 17,
    // many others
};

This represents the IP protocol number in IP packets. 这表示IP数据包中的IP协议号。 Inside a packet, the protocol number is identified by a number rather than a name (8 bits at byte offset 9). 在数据包内部,协议号由数字而不是名称 (字节偏移量9处为8位)标识。 This enumeration lets the program source refer to names like icmp and tcp rather than numbers. 通过此枚举,程序源可以引用icmptcp类的名称,而不是数字。 The number must be used inside the IP packet itself. 该号码必须在IP数据包本身内部使用。

When you use an enum the values (such as black , green , etc.) are symbolic names that are represented internally as integers. 使用enum这些值(例如blackgreen等)是内部以整数表示的符号名称。

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. 此外, enum 只能包含预定义的值,这在大多数情况下都是理想的。 If you represented the same thing as string there's no such guarantee. 如果您表示的内容与字符串相同,则无法保证。

An enumerator (like black in your example) is represented by an integer; 枚举数(例如您的示例中的black )由整数表示; 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. 如果您使用了诸如"black""blue"类的字符串,则无法保证某人在期望使用某种颜色时不会通过您的"hot dog" If you take a color_t , you are guaranteed to get a valid color (unless someone has gone and done something wrong). 如果使用color_t ,则可以保证获得有效的颜色(除非有人走了,做错了什么)。

1) Enum vs Integers: Enums are easier to read and hence code is more maintenable. 1)枚举与整数:枚举更易于阅读,因此代码更易于维护。

2) Enum vs Strings: Enums are much more efficient (alsmo as effecient as integers). 2)枚​​举与字符串:枚举效率更高(等效于整数)。

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. 这也意味着它们仅在值范围较小时才有用。

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." 为了便于记录,Bjarne Stroustrup在《 C ++ 的设计和排除 》一书中谈到了将enum S引入C ++, 声明“ C枚举构成了一个奇怪的半生半熟的概念。枚举不是C原始概念的一部分,并且显然不愿意对语言的介绍是对那些坚持让符号常量的形式比Cpp的无参数宏更重要的人们的让步。” (section 11.7, "Cpp" refers to the C preprocessor, the rest of the section chronicles the decision to make each enum a separate type in C++ instead of all of them being int s as they originally were in C and early versions of C++). (第11.7节“ Cpp”指的是C预处理程序,本节的其余部分记录了决定使每个enum在C ++中成为一个单独的类型,而不是像它们最初在C和C ++的早期版本中那样将它们都设置为int ) 。

enum s are largely meant as a way of replacing #define s in earlier versions of C. enum主要是用来替换C早期版本中的#define的一种方式。

// based on UNIX file permissions
#define EXECUTE 1
#define WRITE 2
#define READ 4

vs.

const int EXECUTE = 1;
const int WRITE = 2;
const int READ = 4;

vs.

enum File_perms {
    EXECUTE = 1;
    WRITE = 2;
    READ = 4;
};

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: enum确实提供了一种形式的文档,说明变量应具有哪种值:

int permissions = 4; // Was that file permissions, database permissions, or something else?
File_perms perms = 4;

This is especially helpful in function signatures: 这在函数签名中特别有用:

int fiddle_bits(int, int); // I can never remember if I pass the file permissions as the first or second parameter ...
File_perms fiddle_bits2(File_perms, int);

enum s are allowed in switch statement case labels (as are #define s and const int s): switch语句case标签中允许使用enum#defineconst int ):

switch (perm) {
    case READ:
        ...
    break;
    ...
}

However, note that it is possible to assign numbers to enum s that don't have a labeled value (C++0x adds an enum class that doesn't allow this): 但是,请注意,这可以分配数字来enum s表示没有标记值(C ++ 0x中增加了一个enum class不允许此):

File_perms perm = 7; // a.k.a., File_perms perm = EXECUTE | READ | WRITE;

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. 如果您看到带有以2的幂表示的显式值的enum ,则几乎可以保证将以这种方式使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM