通过给定枚举的所有枚举变体迭代

发布于 2025-02-04 06:35:52 字数 190 浏览 1 评论 0原文

是否有可能迭代给定枚举的所有枚举变体。我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时模样 2025-02-11 06:35:53

考虑制作诸如allkeypresent的助手方法,在下面的示例程序中说明

#include <iostream>
#include <unordered_map>
//declare an enum
enum DIR : int {NORTH, SOUTH, WEST, EAST}; 

/**
 * AllKeysPresent: return true if and only if map contains all enum keys
 */
template<typename T = int> 
bool AllKeysPresent(const std::unordered_map<DIR, T>& myMap)
{
    bool allKeysPresent = true;
    //loop over enum, starting with the first entry up and including the last entry
    for (int key = DIR::NORTH; key <= DIR::EAST; key++)
    {
        if (myMap.find(static_cast<DIR>(key)) == myMap.end())
        {
            allKeysPresent = false;
            break;
        }
    }
    //TODO: check if at least one valid key is present. return false if the map is empty
    return allKeysPresent;
}

int main()
{
    //make a map with all keys
    std::unordered_map<DIR, int> mapWithAllKeys = { {DIR::NORTH, -1}, {DIR::SOUTH, -2}, {DIR::WEST, -3}, {DIR::EAST, -4} };
    std::unordered_map<DIR, int> mapWithSomeKeys = { {DIR::NORTH, -1}, {DIR::SOUTH, -2} };
    std::cout << AllKeysPresent(mapWithAllKeys) << std::endl;
    std::cout << AllKeysPresent(mapWithSomeKeys) << std::endl;
    return 0;
}


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 below

#include <iostream>
#include <unordered_map>
//declare an enum
enum DIR : int {NORTH, SOUTH, WEST, EAST}; 

/**
 * AllKeysPresent: return true if and only if map contains all enum keys
 */
template<typename T = int> 
bool AllKeysPresent(const std::unordered_map<DIR, T>& myMap)
{
    bool allKeysPresent = true;
    //loop over enum, starting with the first entry up and including the last entry
    for (int key = DIR::NORTH; key <= DIR::EAST; key++)
    {
        if (myMap.find(static_cast<DIR>(key)) == myMap.end())
        {
            allKeysPresent = false;
            break;
        }
    }
    //TODO: check if at least one valid key is present. return false if the map is empty
    return allKeysPresent;
}

int main()
{
    //make a map with all keys
    std::unordered_map<DIR, int> mapWithAllKeys = { {DIR::NORTH, -1}, {DIR::SOUTH, -2}, {DIR::WEST, -3}, {DIR::EAST, -4} };
    std::unordered_map<DIR, int> mapWithSomeKeys = { {DIR::NORTH, -1}, {DIR::SOUTH, -2} };
    std::cout << AllKeysPresent(mapWithAllKeys) << std::endl;
    std::cout << AllKeysPresent(mapWithSomeKeys) << std::endl;
    return 0;
}

I 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 like SOUTHWEST, then AllKeysPresent will still work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文