通过给定枚举的所有枚举变体迭代
是否有可能迭代给定枚举的所有枚举变体。我有一个std :: unordered_map< enum,value_type>
,enum variant作为键。我需要检查给定枚举的所有枚举变体是否存在为std :: unordered_map< enum,value_type>
中的键。
Is it possible to iterate through all enum variants of a given enum. I have an std::unordered_map<Enum, Value_Type>
with enum variant as key. I need to check whether all the enum variants of a given enum exist as key in std::unordered_map<Enum, Value_Type>
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑制作诸如
allkeypresent
的助手方法,在下面的示例程序中说明了
enum dir:int {dir_first,北,南,西,东,dir_last};
其中dir_first和dir_last只是虚拟条目,然后将循环迭代更改为
for(int key = dir :: dir :: dir_first+1; key&lt; dir:dir:dir_last; key ++)
。这样,如果某人决定在您的枚举中添加新条目,例如southwest
,那么allkeyspresent
仍然可以正常工作Consider making a helper method like
AllKeyPresent
illustrated in a sample program belowI would also recommend declaring your enum like this
enum DIR : int {DIR_FIRST, NORTH, SOUTH, WEST, EAST, DIR_LAST};
where DIR_FIRST and DIR_LAST are just dummy entries, and change for loop iteration to
for (int key = DIR::DIR_FIRST + 1; key < DIR:DIR_LAST; key++)
. This way, if someone decides to add a new entry to your enum likeSOUTHWEST
, thenAllKeysPresent
will still work