C++字符串到枚举
C++ 中是否有一种简单的方法将字符串转换为枚举(类似于 C# 中的 Enum.Parse
)? switch 语句会非常长,所以我想知道是否有更简单的方法来做到这一点?
编辑:
感谢您的所有回复。我意识到对于我的具体情况有一种更简单的方法。字符串总是包含字符“S”,后跟一些数字,所以我只是这样做
int i = atoi(myStr.c_str() + 1);
,然后对 i 进行了切换。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
std::map
(或unordered_map
)可以轻松做到这一点。不过,填充地图与 switch 语句一样乏味。编辑:从 C++11 开始,填充就很简单:
A
std::map<std::string, MyEnum>
(orunordered_map
) could do it easily. Populating the map would be just as tedious as the switch statement though.Edit: Since C++11, populating is trivial:
使用
std::map
并使用boost::map_list_of
轻松初始化它。例子,
Use
std::map<std::string, Enum>
and useboost::map_list_of
to easily initialize it.Example,
在某处看到这个例子
saw this example somewhere
这对我有用:
this worked for me:
我用的是这个“绝招”> http://codeproject.com/Articles/ 42035/Enum-to-String-and-Vice-Versa-in-C
后
插入
,如果枚举中的值不重复,则它可以正常工作。
代码中的示例
反之亦然
I use this "trick" > http://codeproject.com/Articles/42035/Enum-to-String-and-Vice-Versa-in-C
After
insert
It works fine, if the values in the enum are not duplicates.
Example in code
vice versa
没有“内置方法”,但是有一些方法可以通过将值-名称对存储在数组中来实现此目的
......
There is no "built-in way", but there are ways to achieve this by storing the pair value-name in an array
.....
简而言之:没有。在 C++ 中,枚举是静态值,而不是像 C# 中的对象。我建议您使用带有一些
if else
语句的函数。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.这是不可能的,因为名称在运行时不可用。在编译期间,每个枚举都会替换为相应的整数值。
It is not possible because the names are not available at runtime. During compilation each enum is replaced with the corresponding integer value.
虽然没有直接的解决方案,但有一些可能的解决方法。
看看这个问题:Easy way to use enum types as string in C?
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?
您可以使用宏来尽量减少重复。这是技巧: 枚举、宏、Unicode 和标记粘贴
You can use macro to minimize repeating yourself. Here is the trick: Enums, Macros, Unicode and Token-Pasting
不,您必须使用 if/then 构造,或者使用映射或哈希表或某种其他类型的关联数据结构来促进这一点。
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.
好吧,您可以在之前进行检查:
在“cookie”存在检查之后,您可以通过使用获取它的值:
希望有帮助
well, you can just make check before:
After "cookie" exist check, you can get it value with use:
hope this help
您必须将字符串值(字符链)映射到相应的枚举值(整数)。以下是您在实践中使用
std::map
执行此操作的方法:Demo
输出:
一些注意事项:
.at()
方法,因为如果在映射中找不到键,它不会插入项目。在我看来,CWG 的决定有点不幸,因为这绝对不是什么“例外”,我们不应该支付费用(至少在这种情况下)。You will have to map the string values (chain of characters) to a corresponding enum value (integer). Here's how you would do this in practice using
std::map
:Demo
Output:
A few notes:
.at()
method as it doesn't insert items if it doesn't find the key in the map. It's a bit of an unfortunate decision by the CWG in my view, as this is definitely nothing "exceptional" and we shouldn't have to pay the cost (in this case at least).