<bdo id='3K06C'></bdo><ul id='3K06C'></ul>

  • <legend id='3K06C'><style id='3K06C'><dir id='3K06C'><q id='3K06C'></q></dir></style></legend>

    <small id='3K06C'></small><noframes id='3K06C'>

  • <tfoot id='3K06C'></tfoot>

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

        在 C++ 中枚举的字符串

        String to enum in C++(在 C++ 中枚举的字符串)
          <i id='OZp71'><tr id='OZp71'><dt id='OZp71'><q id='OZp71'><span id='OZp71'><b id='OZp71'><form id='OZp71'><ins id='OZp71'></ins><ul id='OZp71'></ul><sub id='OZp71'></sub></form><legend id='OZp71'></legend><bdo id='OZp71'><pre id='OZp71'><center id='OZp71'></center></pre></bdo></b><th id='OZp71'></th></span></q></dt></tr></i><div id='OZp71'><tfoot id='OZp71'></tfoot><dl id='OZp71'><fieldset id='OZp71'></fieldset></dl></div>

          • <tfoot id='OZp71'></tfoot>
          • <small id='OZp71'></small><noframes id='OZp71'>

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

                  <tbody id='OZp71'></tbody>
                  <bdo id='OZp71'></bdo><ul id='OZp71'></ul>
                  本文介绍了在 C++ 中枚举的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有没有办法将文本文件中的字符串与枚举值相关联?

                  Is there a way to associate a string from a text file with an enum value?

                  问题是:我有一些枚举值作为字符串存储在一个文本文件中,我在满足某些条件时即时读取这些值......现在我想将读取值分配给一个枚举.

                  The problem is: I have a few enum values stored as string in a text file which I read on the fly on meeting some condition... Now I want to assign the read value to an enum.

                  最有效的方法是什么?它不需要是最简单的方法.

                  What is the most effective way to do so? It doesn't need to be the simplest approach.

                  推荐答案

                  您可以设置一个可以反复使用的地图:

                  You can set up a map that you can use over and over:

                  template <typename T>
                  class EnumParser
                  {
                      map <string, T> enumMap;
                  public:
                      EnumParser(){};
                  
                      T ParseSomeEnum(const string &value)
                      { 
                          map <string, T>::const_iterator iValue = enumMap.find(value);
                          if (iValue  == enumMap.end())
                              throw runtime_error("");
                          return iValue->second;
                      }
                  };
                  
                  enum SomeEnum
                  {
                      Value1,
                      Value2
                  };
                  EnumParser<SomeEnum>::EnumParser()
                  {
                      enumMap["Value1"] = Value1;
                      enumMap["Value2"] = Value2;
                  }
                  
                  enum OtherEnum
                  {
                      Value3, 
                      Value4
                  };
                  EnumParser<OtherEnum>::EnumParser()
                  {
                      enumMap["Value3"] = Value3;
                      enumMap["Value4"] = Value4;
                  }
                  
                  int main()
                  {
                      EnumParser<SomeEnum> parser;
                      cout << parser.ParseSomeEnum("Value2");
                  }
                  

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)

                      <bdo id='cp7HP'></bdo><ul id='cp7HP'></ul>
                      <tfoot id='cp7HP'></tfoot>

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

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