<tfoot id='ULNUm'></tfoot>
  • <legend id='ULNUm'><style id='ULNUm'><dir id='ULNUm'><q id='ULNUm'></q></dir></style></legend>

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

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

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

        C++:将枚举值打印为文本

        C++: Print out enum value as text(C++:将枚举值打印为文本)
      2. <tfoot id='pAejm'></tfoot>

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

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

                  问题描述

                  如果我有这样的枚举

                  enum Errors
                  {ErrorA=0, ErrorB, ErrorC};
                  

                  然后我想打印到控制台

                  Errors anError = ErrorA;
                  cout<<anError;/// 0 will be printed
                  

                  但是我想要的是文本ErrorA",我可以在不使用 if/switch 的情况下做到吗?
                  您对此有何解决方案?

                  but what i want is the text "ErrorA", can i do it without using if/switch?
                  And what is your solution for this?

                  推荐答案

                  使用地图:

                  #include <iostream>
                  #include <map>
                  #include <string>
                  
                  enum Errors {ErrorA=0, ErrorB, ErrorC};
                  
                  std::ostream& operator<<(std::ostream& out, const Errors value){
                      static std::map<Errors, std::string> strings;
                      if (strings.size() == 0){
                  #define INSERT_ELEMENT(p) strings[p] = #p
                          INSERT_ELEMENT(ErrorA);     
                          INSERT_ELEMENT(ErrorB);     
                          INSERT_ELEMENT(ErrorC);             
                  #undef INSERT_ELEMENT
                      }   
                  
                      return out << strings[value];
                  }
                  
                  int main(int argc, char** argv){
                      std::cout << ErrorA << std::endl << ErrorB << std::endl << ErrorC << std::endl;
                      return 0;   
                  }
                  

                  在线性搜索中使用结构数组:

                  Using array of structures with linear search:

                  #include <iostream>
                  #include <string>
                  
                  enum Errors {ErrorA=0, ErrorB, ErrorC};
                  
                  std::ostream& operator<<(std::ostream& out, const Errors value){
                  #define MAPENTRY(p) {p, #p}
                      const struct MapEntry{
                          Errors value;
                          const char* str;
                      } entries[] = {
                          MAPENTRY(ErrorA),
                          MAPENTRY(ErrorB),
                          MAPENTRY(ErrorC),
                          {ErrorA, 0}//doesn't matter what is used instead of ErrorA here...
                      };
                  #undef MAPENTRY
                      const char* s = 0;
                      for (const MapEntry* i = entries; i->str; i++){
                          if (i->value == value){
                              s = i->str;
                              break;
                          }
                      }
                  
                      return out << s;
                  }
                  
                  int main(int argc, char** argv){
                      std::cout << ErrorA << std::endl << ErrorB << std::endl << ErrorC << std::endl;
                      return 0;   
                  }
                  

                  使用开关/案例:

                  #include <iostream>
                  #include <string>
                  
                  enum Errors {ErrorA=0, ErrorB, ErrorC};
                  
                  std::ostream& operator<<(std::ostream& out, const Errors value){
                      const char* s = 0;
                  #define PROCESS_VAL(p) case(p): s = #p; break;
                      switch(value){
                          PROCESS_VAL(ErrorA);     
                          PROCESS_VAL(ErrorB);     
                          PROCESS_VAL(ErrorC);
                      }
                  #undef PROCESS_VAL
                  
                      return out << s;
                  }
                  
                  int main(int argc, char** argv){
                      std::cout << ErrorA << std::endl << ErrorB << std::endl << ErrorC << std::endl;
                      return 0;   
                  }
                  

                  这篇关于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?)
                    <tbody id='oiGg3'></tbody>

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

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

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

                          2. <legend id='oiGg3'><style id='oiGg3'><dir id='oiGg3'><q id='oiGg3'></q></dir></style></legend>