C++枚举数据结构

发布于 2024-09-29 07:02:26 字数 242 浏览 5 评论 0原文

有人可以告诉我对枚举数据结构的实时需求吗?例如在一些真实的系统中可以使用它吗?以及为什么会有这样的数据结构。给出的例子是

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

猛虎独行 2024-10-06 07:02:26

例如,考虑以下枚举:

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

这表示 IP 数据包中的 IP 协议号。在数据包内部,协议号由数字而不是名称(字节偏移9处的8位)来标识。此枚举允许程序源引用诸如 icmp 和 tcp 之类的名称,而不是数字。该编号必须在 IP 数据包本身内部使用。

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. 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 and tcp rather than numbers. The number must be used inside the IP packet itself.

反目相谮 2024-10-06 07:02:26

当您使用enum时,值(例如blackgreen等)是在内部表示为整数的符号名称。

如果您使用字符串数组,那么每次您想要使用其中一个值时,您都需要复制字符串、进行字符串比较等。

此外,enum 可以只包含预定义的值,这在大多数情况下都是可取的。如果您表示与字符串相同的东西,则没有这样的保证。

When you use an enum the values (such as black, 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.

-柠檬树下少年和吉他 2024-10-06 07:02:26

枚举数(如示例中的 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 a color_t, you are guaranteed to get a valid color (unless someone has gone and done something wrong).

左耳近心 2024-10-06 07:02:26

根据记录,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

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

一种

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

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

方式 att.com/~bs/bs_faq2.html#in-class" rel="nofollow">使用哪个很大程度上取决于个人品味。 enum 确实提供了一种关于变量应保存何种值的文档形式:

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

这在函数签名中特别有用:

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);

switch< 中允许 enum /code> 语句 case 标签(例如 #defineconst int):

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

但是,请注意,它是 可以将数字分配给没有标记值的enum(C++0x 添加了一个不允许这样做的enum class):

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

如果您如果你见过一个带有 2 的幂的显式值的 enum,你几乎可以保证它会以这种方式使用。

For the record, Bjarne Stroustrup talks about bringing enums 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 each enum a separate type in C++ instead of all of them being ints as they originally were in C and early versions of C++).

enums are largely meant as a way of replacing #defines in earlier versions of C.

// 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:

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);

enums are allowed in switch statement case labels (as are #defines and const ints):

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

However, note that it is possible to assign numbers to enums that don't have a labeled value (C++0x adds an enum class that doesn't allow this):

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.

つ可否回来 2024-10-06 07:02:26

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文