<i id='Wqlho'><tr id='Wqlho'><dt id='Wqlho'><q id='Wqlho'><span id='Wqlho'><b id='Wqlho'><form id='Wqlho'><ins id='Wqlho'></ins><ul id='Wqlho'></ul><sub id='Wqlho'></sub></form><legend id='Wqlho'></legend><bdo id='Wqlho'><pre id='Wqlho'><center id='Wqlho'></center></pre></bdo></b><th id='Wqlho'></th></span></q></dt></tr></i><div id='Wqlho'><tfoot id='Wqlho'></tfoot><dl id='Wqlho'><fieldset id='Wqlho'></fieldset></dl></div>
      <bdo id='Wqlho'></bdo><ul id='Wqlho'></ul>
  1. <legend id='Wqlho'><style id='Wqlho'><dir id='Wqlho'><q id='Wqlho'></q></dir></style></legend>

    <small id='Wqlho'></small><noframes id='Wqlho'>

    <tfoot id='Wqlho'></tfoot>

    1. C++ 字符串枚举

      时间:2023-08-26

            <small id='muAZ9'></small><noframes id='muAZ9'>

            <legend id='muAZ9'><style id='muAZ9'><dir id='muAZ9'><q id='muAZ9'></q></dir></style></legend>

              <tbody id='muAZ9'></tbody>
            • <i id='muAZ9'><tr id='muAZ9'><dt id='muAZ9'><q id='muAZ9'><span id='muAZ9'><b id='muAZ9'><form id='muAZ9'><ins id='muAZ9'></ins><ul id='muAZ9'></ul><sub id='muAZ9'></sub></form><legend id='muAZ9'></legend><bdo id='muAZ9'><pre id='muAZ9'><center id='muAZ9'></center></pre></bdo></b><th id='muAZ9'></th></span></q></dt></tr></i><div id='muAZ9'><tfoot id='muAZ9'></tfoot><dl id='muAZ9'><fieldset id='muAZ9'></fieldset></dl></div>
                <bdo id='muAZ9'></bdo><ul id='muAZ9'></ul>
                <tfoot id='muAZ9'></tfoot>
                本文介绍了C++ 字符串枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                C++ 中是否有一种简单的方法可以将字符串转换为枚举(类似于 C# 中的 Enum.Parse)?switch 语句会很长,所以我想知道是否有更简单的方法来做到这一点?

                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?

                感谢您的所有回复.我意识到对于我的特殊情况,有一种更简单的方法可以做到这一点.字符串总是包含字符 'S' 后跟一些数字,所以我只是做了

                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);
                

                然后打开 i.

                推荐答案

                A std::map(或 unordered_map)可以做它很容易.填充地图和 switch 语句一样乏味.

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

                编辑:从 C++11 开始,填充是微不足道的:

                Edit: Since C++11, populating is trivial:

                static std::unordered_map<std::string,E> const table = { {"a",E::a}, {"b",E::b} };
                auto it = table.find(str);
                if (it != table.end()) {
                  return it->second;
                } else { error() }
                

                这篇关于C++ 字符串枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:我应该使用#define、enum 还是const? 下一篇:C++ 枚举是有符号的还是无符号的?

                相关文章

                <tfoot id='a1mCe'></tfoot>

              • <small id='a1mCe'></small><noframes id='a1mCe'>

              • <i id='a1mCe'><tr id='a1mCe'><dt id='a1mCe'><q id='a1mCe'><span id='a1mCe'><b id='a1mCe'><form id='a1mCe'><ins id='a1mCe'></ins><ul id='a1mCe'></ul><sub id='a1mCe'></sub></form><legend id='a1mCe'></legend><bdo id='a1mCe'><pre id='a1mCe'><center id='a1mCe'></center></pre></bdo></b><th id='a1mCe'></th></span></q></dt></tr></i><div id='a1mCe'><tfoot id='a1mCe'></tfoot><dl id='a1mCe'><fieldset id='a1mCe'></fieldset></dl></div>
                <legend id='a1mCe'><style id='a1mCe'><dir id='a1mCe'><q id='a1mCe'></q></dir></style></legend>

                      <bdo id='a1mCe'></bdo><ul id='a1mCe'></ul>