Home > Mobile >  Get enum key from unordered map by string value
Get enum key from unordered map by string value

Time:12-18

I need a function that needs to check if the input (std::string) is unique and return its corresponding enum value.

I already have been able to implement this function with just a simple vector, which checks if the input is unique.

it should return enumE::HELLO.

I tried to adapt the code above for the vector to suit this function, but I am not really getting anywhere.

CodePudding user response:

Based on the description of your example, here is a quick implementation. Of course, the logic may not be entirely what you wrote, but I am sure you can tweak it.

  • Why would you iterate through the whole string if std::string has a std::string::find function which will find a substring for you?
  • Is it necessary to go through all the trouble you wrote above? Does the code need to be that dense and unreadable?

Feel free to play around with that function and implement your logic.

  • Related