简体   繁体   English

在C ++中显示,更新和验证枚举值

[英]Displaying, updating and validating values of enum in C++

#include <iostream>

enum SEX {MALE, FEMALE};

int main(int argc, char**argv)
{
    enum SEX the_sex = MALE;
    return 0;
}

How can I display the_sex value on the terminal or console, accept values from the terminal or console to update the value of the_sex and how can I valid the input for the_sex variable? 如何在终端或控制台上显示the_sex值,接受来自终端或控制台的值以更新the_sex的值以及如何验证the_sex变量的输入?

How can I accept values from the terminal or console to update the value of the_sex and how can I valid the input for the_sex variable? 如何接受来自终端或控制台的值以更新the_sex的值以及如何the_sex变量的输入?

The input can be represented by whatever you want: an integer (1 for male, 2 for female), a char ('M' for male, 'F' for female), a std::string . 输入可以用你想要的任何东西来表示:一个整数(男性为1,女性为2), char (男性为“M”,女性为“F”), std::string Here's a code example for the char version: 这是char版本的代码示例:

char in;
std::cin >> in;
switch (in) {
case 'M':
    the_sex = MALE;
    break;
case 'F':
    the_sex = FEMALE;
    break;
default:
    // invalid input
    break;
}

or, here's the std::string version: 或者,这是std::string版本:

std::string in;
std::cin >> in;
        if (in == "MALE") the_sex = MALE;
else    if (in == "FEMALE") the_sex = FEMALE;
else    // invalid input

How can I display the_sex value on the terminal or console? 如何在终端或控制台上显示the_sex值?

You can simply use a switch statement to print out the value of your SEX variable: 您只需使用switch语句打印出SEX变量的值:

std::ostream& operator<<(std::ostream& os, SEX the_sex) { 
    switch (the_sex) {
    case MALE:
        os << "MALE";
        break;
    case FEMALE:
        os << "FEMALE";
        break;
    }
    return os;
}

To output the value of the enumeration you can write simply 要输出枚举值,您可以简单地编写

std::cout << the_sex;

The enumerator will be displayed as an integer value (in this case as 1). 枚举器将显示为整数值(在本例中为1)。

To get and validate a value for the enumeration you can use for example the following loop 要获取并验证枚举的值,您可以使用例如以下循环

int e;

do
{
   std::cout << "Enter the sex of the person (0 - FEMALE, 1 - MALE): ";
} while ( std::cin >> e && e != 0 && e != 1 );

if ( std::cin ) the_sex = static_cast<SEX>( e );

i'm using a macro for doing this. 我正在使用宏来做这件事。

#define name_num(NAME, ...)                                                    \
class NAME {                                                                   \
                                                                               \
public:                                                                        \
                                                                               \
  enum   enums{NAME_NUM_BEGIN_OF_ENUM_MAP,                                     \
               __VA_ARGS__,                                                    \
               NAME_NUM_END_OF_ENUM_MAP};                                      \
                                                                               \
  using  map_type = boost::bimap<enums, std::string>;                          \
                                                                               \
  NAME(std::string const& str) {                                               \
    std::vector<std::string> v;                                                \
    boost::split(v, str, boost::is_any_of(", "), boost::token_compress_on);    \
    map_type m;                                                                \
                                                                               \
    for(int i=NAME_NUM_BEGIN_OF_ENUM_MAP+1; i!=NAME_NUM_END_OF_ENUM_MAP; i++)  \
      map_.insert(map_type::value_type(static_cast<enums>(i), v[i-1]));        \
  }                                                                            \
                                                                               \
  std::string string(enums val)        { return map_.left.at(val); }           \
                                                                               \
  enums number(std::string const& val) { return map_.right.at(val); }          \
                                                                               \
private:                                                                       \
  map_type map_;                                                               \
} NAME(#__VA_ARGS__)

it creates a usual enum list which can be used as usual (eg in switches). 它创建了一个通常的枚举列表,可以照常使用(例如在开关中)。 also it uses boost bimap to map the enums with the coresponding strings. 它还使用boost bimap来映射具有相应字符串的枚举。

the first parameter of the macro is the name of the class and instance which is used to access the enums and the methods. 宏的第一个参数是用于访问枚举和方法的类实例的名称。

finding the enum you use number and finding the string you use string method. 找到你使用number的枚举并找到你使用string方法的string if string (in method number) isn't pointing to a valid enum a std::out_of_range("bimap<>: invalid key") will be thrown. 如果string(在方法编号中)没有指向有效的枚举,则抛出std::out_of_range("bimap<>: invalid key")

see this example. 这个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM