正确使用枚举 C++
我一直在尝试学习如何在 C++ 中正确使用枚举,但我几乎不明白如何处理它们。我制作了一个简单的程序,使用枚举和按位运算来更改交通信号灯:
#include <iostream>
enum lights
{
green = 1,
yellow = 2,
red = 4,
control = 7
};
std::string change_light (std::string choice)
{
lights light;
int changed;
if (choice == "yellow")
light = yellow;
else if (choice == "red")
light = red;
else if (choice == "green")
light = green;
changed = control & light;
if (changed == red)
return "red";
else if (changed == yellow)
return "yellow";
else if (changed == green)
return "green";
}
int main()
{
std::string choice = "";
while (1)
{
std::cout << "What light do you want to turn on?" << std::endl;
std::cin >> choice;
std::cout << "Changed to " << change_light(choice) << std::endl;
}
return 0;
}
如何在保留按位运算和枚举的使用的同时改进该程序?如果您能告诉我如何改进它,这将极大地提高我对如何正确使用枚举的理解。
谢谢:D
I've been trying to learn how to use enums correctly in C++, and I can barely understand how to handle them. I made a simple program that changes traffic lights using enums and bitwise operations:
#include <iostream>
enum lights
{
green = 1,
yellow = 2,
red = 4,
control = 7
};
std::string change_light (std::string choice)
{
lights light;
int changed;
if (choice == "yellow")
light = yellow;
else if (choice == "red")
light = red;
else if (choice == "green")
light = green;
changed = control & light;
if (changed == red)
return "red";
else if (changed == yellow)
return "yellow";
else if (changed == green)
return "green";
}
int main()
{
std::string choice = "";
while (1)
{
std::cout << "What light do you want to turn on?" << std::endl;
std::cin >> choice;
std::cout << "Changed to " << change_light(choice) << std::endl;
}
return 0;
}
How do I improve that program while keeping the use of bitwise operations and enums? If you could show me how to improve it, it would greatly improve my understanding of how to use enums correctly.
Thanks :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
枚举背后的整个想法是,您可以定义一组常量,为用户和编译器提供有关如何使用变量的一些提示。
如果change_light函数采用如下所示的灯光参数,那么您的示例会更有意义:
这样编译器就知道该函数仅采用某些参数,并且您不会得到像change_light(“blue”)这样的函数调用
所以您使用枚举来保护代码的其余部分免受错误参数值的影响。您无法直接从 std::in 读取枚举,因为它不知道有关您的枚举的任何信息。阅读后,您应该将输入转换为枚举。像这样的事情:
从这里开始,所有与交通灯相关的函数只接受枚举值,不需要检查请求值是否在有效范围内。
The whole idea behind enums is, that you can define a set of constants that give the user and compiler some hints about how a variable is to be used.
Your example would make more sense, if the change_light function would take a lights argument like this:
That way the compiler knows, that the function takes only certain arguments and you don't get function calls like change_light("blue")
So you use enums to protect the rest of your code from wrong argument values. You can't directly read an enum from std::in, since it doesn't know anything about your enum. After reading, you should convert the input to an enum. Something like this:
From here on all your traffic light related functions accept only enum values and don't need to check if the request value is within a valid range.
枚举只是具有有限有效值范围的整数。
假设您将使用 int 实现信号量:
以下内容将很好地通过,尽管它破坏了信号量的语义:
使用枚举它不会通过,编译器会捕获错误。
如果您使用 switch 而不是 if/else,效果会更好:
当然,枚举还有其他用途。例如,我个人更喜欢枚举常量而不是定义(实际上从未使用枚举类型本身)。这种情况也是唯一一种可以与按位运算共存的情况,但比较麻烦。
Enums are just integers that have a limited valid value ranges.
Lets say you would implement your semaphore with an int:
The following will pass just fine, although it breaks the semantics of your semaphore:
with an enum it won't pass, compiler with catch the error.
This is even greater if you will use switch instead of if/else:
Of course there are other uses of enums. For example I personally prefer enum constants over define (never actually using the enum type itself). This case is also the only one that can coexist with bitwise operations with much hassle.
您似乎认为与枚举类型关联的数字很重要。事实上,数字根本不重要。枚举类型的真正想法是巧妙地捕获一个变量,该变量具有一组非数字状态,并且使用字符(例如扑克牌花色、指南针)也很难捕获这些状态。方向或一周中的几天)。人们很少(如果有的话)为任何枚举值分配一个特定的数字 - 它是一种不属于枚举类型点的工具(但它偶尔有用,因此被保留)并且更容易混淆初级程序员不应该提供帮助,并且不应将其视为枚举的组成部分 - 有些语言甚至不允许它(例如 Modula-3、Haskell、Ocaml)。枚举类型并不经常使用 - 它们的使用有点晦涩,并且通常不那么有用:它们增加了程序的清晰度,但没有增加功能(相比之下,例如 if 语句,如果没有该语句,编程将无法实现)几乎不可能!)并且实际上从来没有需要。这意味着您编写的代码片段虽然显示了正在使用的枚举类型,但并未证明其有用性:枚举类型在阐明某些较大程序的一个方面方面发挥了作用,而这种用途不容易演示。初学者最好忽略它们,因为它们对初学者来说似乎毫无意义且令人困惑。这是因为它们的目的很难证明——尽管它们很有用,但我向你保证!我再说一遍:它们提供的是清晰的表达,而不是力量。
You appear to have the idea that the numbers associated with the enumerated type are important. In fact the numbers are not important at all. The real idea of the enumerated type is to capture, neatly, a variable that has a set of states which are not numerical and would also be poorly captured by using a character (e.g. playing-card suits, compass directions or days of the week). One would seldom, if ever, assign a specific numbers to any of the enum values - it is a facility that is not part of the point of the enumerated type (but it is very occasionally useful so it is kept) and serves more to confuse the beginning programmer than to assist, and should not be envisaged as an integral part of enumeration - some languages do not even permit it (e.g. Modula-3, Haskell, Ocaml). Enumerated types are not very often used - their use is a bit obscure and they are not often useful: they add clarity to a program but not power (contrast with, for example the if statement without which programming would be nigh impossible!) and are never actually needed as such. This means that the fragment of code that you wrote, while it shows an enumerated type in use, does not demonstrate its usefulness: an enumerated type comes into its own in clarifying an aspect of some larger programs, a use not easily demonstrated. Beginning programmers are best advised to ignore them as they seem pointless and confusing to the beginner. This is because their purpose is hard to demonstrate - though they are useful, I promise you! I repeat: they offer clarity of expression, not power.