生成随机枚举 [英] generating random enums
本文介绍了生成随机枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何为C ++中的枚举类型随机选择一个值?
我想做这样的事情。
How do I randomly select a value for an enum type in C++? I would like to do something like this.
enum my_type(A,B,C,D,E,F,G,h,J,V);
my_type test(rand() % 10);
但这是非法的...没有从int到枚举类型的隐式转换。 / p>
But this is illegal... there is not an implicit conversion from int to an enum type.
推荐答案
如何:
enum my_type {
a, b, c, d,
last
};
void f() {
my_type test = static_cast<my_type>(rand() % last);
}
这篇关于生成随机枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文