<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++:将枚举值打印为文本

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

                  上一篇:在 C++ 中前向声明枚举 下一篇:基本枚举类继承

                  相关文章

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