在枚举元素的数量

在枚举元素的数量

本文介绍了在枚举元素的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C,有一个很好的方法来跟踪一个枚举元素的数量?我见过

In C, is there a nice way to track the number of elements in an enum? I've seen

enum blah {
    FIRST,
    SECOND,
    THIRD,
    LAST
};

但是,如果项目是连续的,从零开始这仅适用。

But this only works if the items are sequential and start at zero.

推荐答案

我不相信有。但是你会用这样的数字做,如果他们不连续的,而你还没有他们的名单的地方?如果他们是连续的,但在不同的数字开始,你总是可以做的:

I don't believe there is. But what would you do with such a number if they are not sequential, and you don't already have a list of them somewhere? And if they are sequential but start at a different number, you could always do:

enum blah {
    FIRST = 128,
    SECOND,
    THIRD,
    END
};
const int blah_count = END - FIRST;

这篇关于在枚举元素的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:03