Convert enum value to std::string in C++ (C++ enum 转 string)

Posted Escape The Well

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Convert enum value to std::string in C++ (C++ enum 转 string)相关的知识,希望对你有一定的参考价值。

C++ enum 转 string
 #include <iostream>
 #include <vector>
 
 #define DEFINE_ENUM(name, ...)\\
 class name \\
 public:\\
     name(size_t v): v_(v) \\
     \\
     enum  __VA_ARGS__ ; \\
     \\
     std::string ToString() \\
         static std::vector<std::string> m = GetMappings();\\
         if (v_ > m.size()) \\
             return #name ":-Invalid";\\
         \\
         return m[v_];\\
     \\
 private:\\
     std::vector<std::string> GetMappings() \\
         const static std::string kEnumName = #name;\\
         std::vector<std::string> tokens;\\
         std::string s = #__VA_ARGS__; \\
         std::string token;\\
         for (char c : s) \\
             if (c == \' \' || c == \',\') \\
                 if (!token.empty()) \\
                     tokens.push_back(kEnumName + "::" + token);\\
                     token.clear();\\
                 \\
              else \\
                 token += c;\\
             \\
         \\
         return tokens;\\
     \\
     \\
 private:\\
     size_t v_;\\
 
 
 DEFINE_ENUM(Weekday, 
     Monday,
     Tuesday, 
 );
 
 int main() 
     Weekday day = Weekday::Tuesday;
     std::cout << day.ToString() << std::endl;
     return 0;
 

打印结果:

Weekday::Tuesday

 

no suitable ctr exists to convert from 'int' to 'std::basic_string<char,std::char_tra

 1 int xfun(int *a,int n)
 2 {
 3     int x = *a;//a的类型是int *,a+1跳动一个int的长度
 4     for (int *pa = a + 1; pa < a + n; pa++)//指向同一个类型的指针比较大小,相减是两者之间的元素个数
 5     {
 6         //string s = pa - a;// string接受const char*的单参构造函数不是explicit的,但编译器不能把int转换为string类型
 7         decltype(pa - a) t;
 8         int arr[3][4];
 9         decltype(arr) Type;
10         float f = pa - a;//因为float是四个字节,有效数字为6为,int转换为float可能丢失精度
11         cout << *pa << endl;
12         if (*pa > x)
13             x = *pa;
14     }
15     return x;
16 }
17 int main()
18 {
19     int x[5] = { 23, 46, 78, 55, 16 };
20     cout<<xfun(x, 5);
21 }
View Code

以上代码可讨论几个问题,记录在下面。这段代码的功能是找出数组中的最大值,用x记录比较过程,x初始为a[0],从第二个元素开始比较,比x大,x值就更新,遍历完数组,x就是最大的。


1、std::string编译器是不认识的,只认识int,float,int*等类型,string在编译器里的类型是std::basic_string<char,std::char_traits<char>,std::allocator<char> > ;

2、pa(搜狗中文输入状态下输入“pa”,按下Enter是选中英文字符,按下空格键是选中“怕”,按下shift是选中英文字符,且切换到英文状态 ),如下图所示:

3、string的单参ctr接受const char*,但不接受int,这个ctr不是explicit的,不能把int隐式转换为string

4、怎么获得两个指针相减的结果类型?即获取表达式的类型,用decltype,不计算表达式的值,只得到类型,如图:

指针相减的类型为ptrdiff_t ,这是一个signed int,如上图所示

arr的类型是二维数组,arr+1是跳一个二维数组的长度,即12个int大小

5、int和float在32位下为4个bytes,float有效数字为6位,int转换为float可能会丢失精度,warning C4244: \'initializing\' : conversion from \'int\' to \'float\', possible loss of data;int赋值给double,后者为8个字节,足够装下所有的int,warning就会消失了。

6、pa<pa+n,pa满足循环条件的最大值为pa+n-1,pa+0就是pa,指向a[1],pa+n-1指向数组的最后一个元素

 

以上是关于Convert enum value to std::string in C++ (C++ enum 转 string)的主要内容,如果未能解决你的问题,请参考以下文章

解决 Unable to convert MySQL date/time value to System.DateTime

MySQL Unable to convert MySQL datetime value to System.DateTime 解决方案

Unable to convert MySQL date/time value to System.DateTime问题解决方案

Uncaught TypeError: Cannot convert object to primitive value

Cannot convert value of type ‘java.lang.String‘ to requi

pandas提示"Cannot convert {0!r} to Excel".format(value)