c++枚举中的最大值和最小值

时间:2022-09-28 17:04:15

Is there a way to find the maximum and minimum defined values of an enum in c++?

是否有办法找到c++中枚举的最大值和最小值?

5 个解决方案

#1


72  

No, there is no way to find the maximum and minimum defined values of any enum in C++. When this kind of information is needed, it is often good practice to define a Last and First value. For example,

不,无法找到c++中任何枚举的最大和最小定义值。当需要这类信息时,通常最好定义最后和第一个值。例如,

enum MyPretendEnum
{
   Apples,
   Oranges,
   Pears,
   Bananas,
   First = Apples,
   Last = Bananas
};

There do not need to be named values for every value between First and Last.

不需要为First和Last之间的每个值命名值。

#2


24  

No, not in standard C++. You could do it manually:

不,不是标准的c++。你可以手动操作:

enum Name
{
   val0,
   val1,
   val2,
   num_values
};

num_values will contain the number of values in the enum.

num_values将包含枚举中的值数量。

#3


4  

No. An enum in C or C++ is simply a list of constants. There is no higher structure that would hold such information.

不。C或c++中的枚举仅仅是一个常量列表。没有更高的结构可以容纳这样的信息。

Usually when I need this kind of information I include in the enum a max and min value something like this:

通常,当我需要这类信息时,我在enum中包含的max和min值是这样的:

enum {
  eAaa = 1,
  eBbb,
  eCccc,
  eMin = eAaaa,
  eMax = eCccc
}

See this web page for some examples of how this can be useful: Stupid Enum Tricks

请参阅这个web页面,以获得一些示例,说明这是如何有用的:愚蠢的Enum技巧

#4


3  

  enum My_enum
    {
       FIRST_VALUE = 0,

       MY_VALUE1,
       MY_VALUE2,
       ...
       MY_VALUEN,

       LAST_VALUE
    };

after definition, My_enum::LAST_VALUE== N+1

定义后,My_enum::LAST_VALUE = = N + 1

#5


-2  

you don't even need them, what I do is just I say for example if you have:

你甚至不需要它们,我所做的就是,举个例子,如果你有

enum Name{val0,val1,val2};

if you have switch statement and to check if the last value was reached do as the following:

如果你有switch语句,并检查最后一个值是否达到如下所示:

if(selectedOption>=val0 && selectedOption<=val2){

   //code
}

#1


72  

No, there is no way to find the maximum and minimum defined values of any enum in C++. When this kind of information is needed, it is often good practice to define a Last and First value. For example,

不,无法找到c++中任何枚举的最大和最小定义值。当需要这类信息时,通常最好定义最后和第一个值。例如,

enum MyPretendEnum
{
   Apples,
   Oranges,
   Pears,
   Bananas,
   First = Apples,
   Last = Bananas
};

There do not need to be named values for every value between First and Last.

不需要为First和Last之间的每个值命名值。

#2


24  

No, not in standard C++. You could do it manually:

不,不是标准的c++。你可以手动操作:

enum Name
{
   val0,
   val1,
   val2,
   num_values
};

num_values will contain the number of values in the enum.

num_values将包含枚举中的值数量。

#3


4  

No. An enum in C or C++ is simply a list of constants. There is no higher structure that would hold such information.

不。C或c++中的枚举仅仅是一个常量列表。没有更高的结构可以容纳这样的信息。

Usually when I need this kind of information I include in the enum a max and min value something like this:

通常,当我需要这类信息时,我在enum中包含的max和min值是这样的:

enum {
  eAaa = 1,
  eBbb,
  eCccc,
  eMin = eAaaa,
  eMax = eCccc
}

See this web page for some examples of how this can be useful: Stupid Enum Tricks

请参阅这个web页面,以获得一些示例,说明这是如何有用的:愚蠢的Enum技巧

#4


3  

  enum My_enum
    {
       FIRST_VALUE = 0,

       MY_VALUE1,
       MY_VALUE2,
       ...
       MY_VALUEN,

       LAST_VALUE
    };

after definition, My_enum::LAST_VALUE== N+1

定义后,My_enum::LAST_VALUE = = N + 1

#5


-2  

you don't even need them, what I do is just I say for example if you have:

你甚至不需要它们,我所做的就是,举个例子,如果你有

enum Name{val0,val1,val2};

if you have switch statement and to check if the last value was reached do as the following:

如果你有switch语句,并检查最后一个值是否达到如下所示:

if(selectedOption>=val0 && selectedOption<=val2){

   //code
}