枚举 C++ 按索引获取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/321801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enum C++ Get by Index
提问by JaredPar
I was wondering in C++ if I have an enum can I access the value at the second index? For example I have
我想知道在 C++ 中是否有枚举可以访问第二个索引处的值吗?例如我有
enum Test{hi, bye};
if I want 'hi', can I do something like Test[0], thanks.
如果我想要“嗨”,我可以做类似 Test[0] 的事情吗,谢谢。
回答by JaredPar
Yes and no. If your Enum does not have explicit values then it is possible. Without an explicit values, enum values are given numeric values 0-N in order of declaration. For example ...
是和否。如果您的 Enum 没有明确的值,那么这是可能的。如果没有明确的值,枚举值会按照声明的顺序给出 0-N 的数值。例如 ...
enum Test {
hi, // 0
bye // 1
}
This means that indexes just translates into a literal value.
这意味着索引只是转换为文字值。
Test EnumOfIndex(int i) { return static_cast<Test>(i); }
This of course does 0 validation at runtime and as soon as you add an explicit value it will break down. But it will work in the default scenario.
这当然会在运行时进行 0 验证,一旦您添加显式值,它就会崩溃。但它会在默认情况下工作。
回答by xan
Unless specified otherwise, enums start numbering at 0, and increment by 1 each entry.
除非另有说明,否则枚举从 0 开始编号,每个条目递增 1。
enum Test
{
hi, //0
bye, //1
count //2
}
You can cast an int to the type of the enum to get the value you want, such as:
您可以将 int 转换为 enum 的类型以获得您想要的值,例如:
(Test)0;
//or
Test(0);
Which lets you do things like:
这让您可以执行以下操作:
for(int i = 0; i < count; i++)
{
DoSomething((Test)i);
}
回答by strager
Enumerations map names to values. In your case, (int)hi would have a value of 0, and (int)bye a value of 1. You can use a cast to get the value of hi:
枚举将名称映射到值。在您的情况下, (int)hi 的值为 0,而 (int)bye 的值为 1。您可以使用强制转换来获取 hi 的值:
int myInteger = 0;
Test myValue = (Test)myInteger;
Note, though, that myValue could be an invalid enum value if myInteger is out of range.
但请注意,如果 myInteger 超出范围,则 myValue 可能是无效的枚举值。
回答by Rob Prouse
No, but you could cast from int
不,但你可以从 int 投射
Test test = (Test)0;
回答by Steve Jessop
Depends what you mean by "I want 'hi'".
取决于你所说的“我想要'嗨'”是什么意思。
If you mean you want the value, then you can get it by casting an int, as others have said.
如果你的意思是你想要这个值,那么你可以通过转换一个 int 来获得它,正如其他人所说。
Casting a numeric literal to enum type is usually pointless - if you know which value you're expecting, you can use the name. That way, the enum can change without breaking your code. I guess it's possible that something really weird is going on, where someone has created an enum, and documented what "3" means but not which enum value it is. But then you'd want to fix the API.
将数字文字转换为枚举类型通常是没有意义的 - 如果您知道期望的值,则可以使用该名称。这样,枚举可以更改而不会破坏您的代码。我想有可能发生了一些非常奇怪的事情,有人创建了一个枚举,并记录了“3”的含义,但没有记录它是哪个枚举值。但是你会想要修复 API。
Casting an integer value known at runtime to enum might be helpful if you have serialized data. As long as you know it's in range of the enum, the result is defined.
如果您有序列化数据,将运行时已知的整数值转换为 enum 可能会有所帮助。只要你知道它在枚举的范围内,结果就被定义了。
If you mean you want the string "hi", then you can't have it. Unlike Java, in C++ the names of the values in enumerated types exist only at compile time, not at runtime, and only map in one direction.
如果你的意思是你想要字符串“hi”,那么你不能拥有它。与 Java 不同,在 C++ 中,枚举类型中的值的名称仅在编译时存在,而不在运行时存在,并且仅在一个方向上映射。
回答by Chavan Maharshi
If you are excepting the value to returned as {Hi or bye} ,then you cannot get the value like that .
如果您将返回的值排除在 {Hi 或 bye} 之外,那么您将无法获得这样的值。
i would not suggest this to be done inorder to get the actual value but it can be used as hack
我不建议这样做以获得实际价值,但它可以用作黑客
string return_value(int index)
{
string temp = "";
switch (index)
{
case 1: temp = "hi"
break;
case 2: temp = "bye";
break;
defualt :
break;
}
return temp;
}
typecasting to enum would again return the index but you can assign to some other enum variable
对 enum 的类型转换将再次返回索引,但您可以分配给其他一些 enum 变量
回答by An???drew
Your best option might be something like this:
您最好的选择可能是这样的:
enum Test{hi = 0, bye};
Then you can simply refer to 'hi' with the number 0, and 'bye' with 1.
然后,您可以简单地用数字 0 表示“hi”,用 1 表示“bye”。
Although this really defeats the whole purpose of using an enumeration in the first place.
尽管这确实违背了首先使用枚举的全部目的。