c/c++语言开发共享C++把数字转为枚举类型的方法

一种方法使用swicth语句, string getelemtypename(elemtype type) { switch(type) { case e

一种方法使用swicth语句,

  string getelemtypename(elemtype type)  {      switch(type)      {          case elemtype::cap : return "cap";break;          case elemtype::ind : return "ind";break;          case elemtype::vs :  return "vs";break;          default: return "error"; break;          }  }  另一种方法,定义常量字符串数组,  const char* names[] = {"cap","ind","vs"};  string getelemtypename(elemtype type)  {      int idx = static_cast<int>(type);      return names[idx];  }

网上还有其他使用宏定义之类的方法,我觉得这两种方法够用就可以了。

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/607441.html

(1)
上一篇 2021年5月14日
下一篇 2021年5月14日

精彩推荐