C++ string to enum

匿名 (未验证) 提交于 2019-12-03 08:52:47

问题:

Is there a simple way in C++ to convert a string to an enum (similar to Enum.Parse in C#)? A switch statement would be very long, so I was wondering if there is a simpler way to do this?

EDIT:

Thanks for all of your replies. I realized that there was a much simpler way to do it for my particular case. The strings always contained the charater 'S' followed by some number so i just did

int i = atoi(myStr.c_str() + 1); 

and then did a switch on i.

回答1:

A std::map<:string myenum=""> (or unordered_map) could do it easily. Populating the map would be just as tedious as the switch statement though.



回答2:

Use std::map<:string enum=""> and use boost::map_list_of to easily initialize it.

Example,

enum X {    A,    B,    C };  std::map<:string x=""> xmap = boost::map_list_of("A", A)("B", B)("C",C); 


回答3:

saw this example somewhere

#include  #include   enum responseHeaders {     CONTENT_ENCODING,     CONTENT_LENGTH,     TRANSFER_ENCODING, };  // String switch paridgam    struct responseHeaderMap : public std::map<:string responseheaders=""> {     responseHeaderMap()     {         this->operator[]("content-encoding") =  CONTENT_ENCODING;         this->operator[]("content-length") = CONTENT_LENGTH;         this->operator[]("transfer-encoding") = TRANSFER_ENCODING;     };     ~responseHeaderMap(){} }; 


回答4:

There is no "built-in way", but there are ways to achieve this by storing the pair value-name in an array

enum myEnum {     enumItem0,     enumItem1,     enumItem7 = 7,     enumItem8 };  std::vector<:pair>>   gMap;  #define ADDITEM(x)  gMap.push_back(std::pair(x,#x)); 

.....

ADDITEM(enumItem0); ADDITEM(enumItem1); ADDITEM(enumItem7); ADDITEM(enumItem8); 


回答5:

I use this "tricks" > http://codeproject.com/Articles/42035/Enum-to-String-and-Vice-Versa-in-C

After

enum FORM {     F_NONE = 0,     F_BOX,     F_CUBE,     F_SPHERE, }; 

insert

Begin_Enum_String( FORM ) {     Enum_String( F_NONE );     Enum_String( F_BOX );     Enum_String( F_CUBE );     Enum_String( F_SPHERE ); } End_Enum_String; 

Work fine, if values in enum are not dublicate.

Example in code

enum FORM f = ... const std::string& str = EnumString::From( f ); 

vice versa

assert( EnumString::To( f, str ) ); 


回答6:

In short: there is none. In C++ enums are static values and not objects like in C#. I suggest you use a function with some if else statements.



回答7:

You can use macro to minimize repeating yourself. Here is the trick: Enums, Macros, Unicode and Token-Pasting



回答8:

It is not possible because the names are not available at runtime. During compilation each enum is replaced with the corresponding integer value.



回答9:

While there is no direct solution, there are a few possible workarounds.

Take a look at this question: Easy way to use variables of enum types as string in C?



回答10:

No, you'll have to use an if/then construction, or use a map or hash table or some other type of associative data structure to facilitate this.



回答11:

this worked for me:

enum NODES { Cone = 1, BaseColor = 2, NONE = 0 };  std::map<:string nodes=""> nodeMap; nodeMap["Cone"] = NODES::Cone; nodeMap["BaseColor"] = NODES::BaseColor; 


回答12:

well, you can just make check before:

auto it = responseHeaderMap.find("cookie"); if (it != responseHeaderMap.end()) {      // "cookie" exist, can take value  } 

After "cookie" exist check, you can get it value with use:

responseHeaderMap["cookie"] 

hope this help



文章来源: C++ string to enum
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!