<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++ 中枚举的字符串

        时间:2023-08-26
          <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++ 中枚举的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 C++ 中枚举枚举 下一篇:检测多个枚举项何时映射到相同的值

                  相关文章

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

                  1. <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>