Mapping, updating and checking enum values ​​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 a terminal or console, accept values ​​from a terminal or console to update the_sex value, and how can I enter a value for the_sex variable?

+4
source share
3 answers

How can I accept values ​​from a terminal or console to update the_sex value and how can I enter a value for a variable the_sex?

The input can be represented by any one you want: an integer (1 for men, 2 for women), a char('M' for men, 'F' for women), a std::string. Here is the sample code for the 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 is the 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

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;
}
+2

,

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

.

#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 .

- , .

find the enumeration you are using numberand find the line where you are using the method string. if the line (in the method number) does not indicate a valid enumeration a std::out_of_range("bimap<>: invalid key").

see this example.

+2
source

All Articles