访问使用C中的索引枚举 [英] Access an enum using an index in C
本文介绍了访问使用C中的索引枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑:
enum Test
{
a = 3,
b = 7,
c = 1
};
我想用一个索引来访问枚举。事情是这样的:
I want to access the enum using an index. Something like this:
for (i=0; i<n; i++)
doSomething((Test)i);
我如何做这样的事情,在这里我将能够使用一个索引来访问枚举,但枚举的成员有不同的价值观?
How can I do something like this, where I will be able to access the enum using an index, though the members of the enum have different values?
推荐答案
这是你能做的最好的:
enum Test { a = 3, b = 7, c = 1, LAST = -1 };
static const enum Test Test_map[] = { a, b, c, LAST };
for (int i = 0; Test_map[i] != LAST; i++)
doSomething(Test_map[i]);
您必须自己保持的映射。
You have to maintain the mapping yourself.
这篇关于访问使用C中的索引枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文