📜  cin une énumération - C++ 代码示例

📅  最后修改于: 2022-03-11 14:44:54.419000             🧑  作者: Mango

代码示例1
#include 
#include 

using namespace std;

enum X { A, B, C };

istream& operator>> ( istream& in, X& x )
{
  int val;

  if ( in>> val ) {
    switch ( val ) {
    case A: case B: case C:
      x = X(val); break;
    default:
      throw out_of_range ( "Invalid value for type X" );
    }
  }

  return in;
}

int main()
{
  X x;

  try {
    cin>> x;
    cout<< x <