繁体   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;
}

如何在终端或控制台上显示the_sex值,接受来自终端或控制台的值以更新the_sex的值以及如何验证the_sex变量的输入?

如何接受来自终端或控制台的值以更新the_sex的值以及如何the_sex变量的输入?

输入可以用你想要的任何东西来表示:一个整数(男性为1,女性为2), char (男性为“M”,女性为“F”), std::string 这是char版本的代码示例:

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

或者,这是std::string版本:

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

如何在终端或控制台上显示the_sex值?

您只需使用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;
}

要输出枚举值,您可以简单地编写

std::cout << the_sex;

枚举器将显示为整数值(在本例中为1)。

要获取并验证枚举的值,您可以使用例如以下循环

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 );

我正在使用宏来做这件事。

#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__)

它创建了一个通常的枚举列表,可以照常使用(例如在开关中)。 它还使用boost bimap来映射具有相应字符串的枚举。

宏的第一个参数是用于访问枚举和方法的类实例的名称。

找到你使用number的枚举并找到你使用string方法的string 如果string(在方法编号中)没有指向有效的枚举,则抛出std::out_of_range("bimap<>: invalid key")

这个例子。

暂无
暂无

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

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