In my experience, the real world rarely provides for indexes of nonnegative integers. Many things aren't even represented numerically. And many things with a numerically-represented index don't begin their indexes at 0. Why then are we still limited to integer-indexed arrays?
根据我的经验,现实世界很少提供非负整数的索引。许多事情甚至没有用数字表示。许多具有数字表示索引的东西不会将其索引开始为0.为什么我们仍然只限于整数索引数组?
Maybe I'm wrong, but it seems like enum indexed arrays are often more appropriate than numerically-indexed arrays (as enums are often more accurate, "real-world" representations). While enums can often be translated into C-style array indices with relative ease...
也许我错了,但似乎枚举索引数组通常比数字索引数组更合适(因为枚举通常更准确,“真实世界”表示)。虽然枚举通常可以相对轻松地转换为C风格的数组索引...
enum Weekday = {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
}
// hopefully C doesn't allow nonsequential enum values; else pray to God
// no one does something like setting Sunday = 4 and Saturday = 4096
int numberOfDays = Saturday-Sunday+1;
int hoursWorkedPerDay[numberOfDays];
hoursWorkedPerDay[(int)SUNDAY] = 0;
hoursWorkedPerDay[(int)MONDAY] = 8;
hoursWorkedPerDay[(int)TUESDAY] = 10;
hoursWorkedPerDay[(int)WEDNESDAY] = 6;
hoursWorkedPerDay[(int)THURSDAY] = 8;
hoursWorkedPerDay[(int)FRIDAY] = 8;
hoursWorkedPerDay[(int)SATURDAY] = 0;
...we are still required to maintain consistency between the number of enums and the size of the array (however, this isn't an awful solution because there's not a more valid integer mapping for "SUNDAY" than 0), and more importantly, anything that can be cast to an int could still be dropped into the index to manipulate the array:
...我们仍然需要保持枚举数和数组大小之间的一致性(但是,这不是一个糟糕的解决方案,因为“SUNDAY”没有比0更有效的整数映射,更重要的是,任何可以强制转换为int的东西仍然可以放入索引来操作数组:
// continued from above
void resetHours (void) {
int i = 0;
int hours = 0;
for (i = 0; i<numberOfDays; i++) {
hoursWorkedPerDay[hours] = i;
// oops, should have been: "...[i] = hours;"
// an enum-indexed implementation would have caught this
// during compilation
}
}
Furthermore, the entire conversion from enum to int is an entire layer of complexity that seems unnecessary.
此外,从enum到int的整个转换是整个复杂层,似乎是不必要的。
Can someone please explain whether there is validity to enum-indices, and list some pros and cons to each approach? And perhaps why a feature so seemingly useful is missing from the C standard, if such information exists?
有人可以解释一下enum-indices是否有效,并列出每种方法的优缺点?也许为什么如果存在这样的信息,C标准中缺少一个看似有用的功能?
2 个解决方案
#1
3
If you really want to use the enum as index you should declare the integer values explicitly.
如果你真的想使用枚举作为索引,你应该明确声明整数值。
On the other hand I personally would prefer s.th. type safe (i.e. without the ugly cast, that might even not be necessary), like:
另一方面,我个人更喜欢s.th.类型安全(即没有丑陋的演员,甚至可能没有必要),如:
std::map<Weekday,int> hoursWorkedPerDay;
#2
2
Sunday =0 //by default, if you won't mention explicit value then it would take 0
and Saturday = 6 // as in your example
和星期六= 6 //如你的例子
so
int numberOfDays = Saturday-Sunday; // which is 6
int hoursWorkedPerDay[numberOfDays];
array will have only 6 places to hold the value.
数组只有6个位置来保存该值。
hoursWorkedPerDay[(int)SUNDAY] = 0;
hoursWorkedPerDay[(int)MONDAY] = 8;
hoursWorkedPerDay[(int)TUESDAY] = 10;
hoursWorkedPerDay[(int)WEDNESDAY] = 6;
hoursWorkedPerDay[(int)THURSDAY] = 8;
hoursWorkedPerDay[(int)FRIDAY] = 8;
hoursWorkedPerDay[(int)SATURDAY] = 0;
accessing out of array index (which is 6) is undefined behaviour
访问数组索引(即6)是未定义的行为
#1
3
If you really want to use the enum as index you should declare the integer values explicitly.
如果你真的想使用枚举作为索引,你应该明确声明整数值。
On the other hand I personally would prefer s.th. type safe (i.e. without the ugly cast, that might even not be necessary), like:
另一方面,我个人更喜欢s.th.类型安全(即没有丑陋的演员,甚至可能没有必要),如:
std::map<Weekday,int> hoursWorkedPerDay;
#2
2
Sunday =0 //by default, if you won't mention explicit value then it would take 0
and Saturday = 6 // as in your example
和星期六= 6 //如你的例子
so
int numberOfDays = Saturday-Sunday; // which is 6
int hoursWorkedPerDay[numberOfDays];
array will have only 6 places to hold the value.
数组只有6个位置来保存该值。
hoursWorkedPerDay[(int)SUNDAY] = 0;
hoursWorkedPerDay[(int)MONDAY] = 8;
hoursWorkedPerDay[(int)TUESDAY] = 10;
hoursWorkedPerDay[(int)WEDNESDAY] = 6;
hoursWorkedPerDay[(int)THURSDAY] = 8;
hoursWorkedPerDay[(int)FRIDAY] = 8;
hoursWorkedPerDay[(int)SATURDAY] = 0;
accessing out of array index (which is 6) is undefined behaviour
访问数组索引(即6)是未定义的行为