0

I have a enum Obs which contains 170+ different types and was wondering how I can compare/assign a string input (from a file) to the appropriate enum.

For example, say I have an enum that looks like

enum Obs {A1 = 1, B1, C1, A2, B2, C2, A3, B3, C3};

And I read this line in from the file

"A1 B2 C3"

If I parse the elements of the line to get the obs type, is there a way to convert that to the enum value. I know I could do it with a bunch of if statements but was wondering if there something easier. The line from the file will be changing (dont know how many obs or what they are) but they will also be the exact same notation as the notation of the obs in the enum.

EDIT As it seems this has been answered or clarifed and the solution appears to use a map; perhaps someone can clarify how to predefine a map. What I am trying is

std::map<std::string, int> ObsMap = 
{ {"A1", 1}, {"B1", 2}....};

however it is not allowing me to do it. Also, how would I access the value of 1 (from A1) by passing it a string from a file: Basically want to pass into my map "A1" and return the int value of 1.

user2840470
  • 919
  • 1
  • 11
  • 23

1 Answers1

0

I did this at my old job. I know it is easy in C# so I assumed the same but there is not really a supported method. The best way is to make a std::map<std::string, MyEnum> of each Enum then find it in the list.

marsh
  • 2,592
  • 5
  • 29
  • 53