我可以使用std::map,其中键和值都是c++ 11中的枚举类吗?

Can I use an std::map with both keys and values being enum classes in C++11?

本文关键字:c++ 枚举 键和值 std 可以使 map 我可以      更新时间:2023-10-16

我需要一种将c++ 11 enum classes映射到其他enum classes的方法。假设我有这些c++ 11风格的枚举:

#include <iostream>
#include <map>
class Foobar
{
public:
    enum class Lecture
    {
        COMP_SCI,
        PHYSICS,
        CHEMISTRY,
        BIOLOGY,
        PSYCHOLOGY,
        MODERN_ART,
        PHILOSOPHY,
        SOCIOLOGY
    };
    enum class Day
    {
        MONDAY,
        TUESDAY,
        WEDNESDAY,
        THURSDAY,
        FRIDAY,
        SATURDAY,
        SUNDAY
    };
};

void test_enum_class_to_enum_class_std_map()
{
    std::map<Foobar::Lecture, Foobar::Day> lectureToDayMap;
    lectureToDayMap.insert(Foobar::Lecture::COMP_SCI, Foobar::Day::MONDAY);
}
int main(int argc, char** argv)
{
    test_enum_class_to_enum_class_std_map();
    return 0;
}

是否有从一个枚举类映射到另一个枚举类的方法?

我想要像

这样的东西

std::map<Foobar::Lecture, Foobar::Day> lectureToDayMap;

如果这是不可能的,我是否能够从int转换为enum class,反之亦然,所以我可以使用std::map<int, int>映射并转换为int插入,并返回到enum class检索值?

当我尝试第一个方法时,这里是编译错误:

test_cpp11_enums.cpp: In function ‘void test_enum_class_to_enum_class_std_map()’:
test_cpp11_enums.cpp:51:74: error: no matching function for call to ‘std::map<Foobar::Lecture, Foobar::Day>::insert(Foobar::Lecture, Foobar::Day)’
     lectureToDayMap.insert(Foobar::Lecture::COMP_SCI, Foobar::Day::MONDAY);
                                                                          ^
test_cpp11_enums.cpp:51:74: note: candidates are:
In file included from /usr/include/c++/4.8/map:61:0,
                 from test_cpp11_enums.cpp:9:
/usr/include/c++/4.8/bits/stl_map.h:594:7: note: std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(const value_type&) [with _Key = Foobar::Lecture; _Tp = Foobar::Day; _Compare = std::less<Foobar::Lecture>; _Alloc = std::allocator<std::pair<const Foobar::Lecture, Foobar::Day> >; typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator = std::_Rb_tree_iterator<std::pair<const Foobar::Lecture, Foobar::Day> >; std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair<const Foobar::Lecture, Foobar::Day>]
       insert(const value_type& __x)

感谢您的宝贵时间:)

编辑1:修正了缺少逗号

编辑2:添加了编译错误I get

编辑3:添加了我正在尝试的完整源代码

编辑4:没有新的代码或任何东西,只是为误解这些帖子的工作方式向每个人道歉。我仍然很难理解编译错误,我也没有在谷歌上找到我想要的东西。我认为直接询问我想要什么可能会更容易,也许会得到关于类枚举如何与地图一起工作(或不工作)的解释。我将继续玩我的测试源代码,并继续在谷歌上寻找。(

)

编辑5:你们很好地告诉我我的错误在哪里,谢谢。我没有看到sdt::mapinsert函数没有将键和值作为参数。起初,我认为这个错误意味着没有std::map命名的插入存在,当我检查它是否在线时,我不知怎的错过了查看参数,确信这与enum classes有关,因为我以前从未使用过它们。

根据http://en.cppreference.com/w/cpp/container/mapstd::map是一个排序的关联容器,其中包含具有唯一键的键值对。因此必须插入一对。

代替

 lectureToDayMap.insert(Foobar::Lecture::COMP_SCI, Foobar::Day::MONDAY);

mymap.insert(std::make_pair(Foobar::Lecture::COMP_SCI, Foobar::Day::MONDAY));

或更好

lectureToDayMap[Foobar::Lecture::COMP_SCI] = Foobar::Day::MONDAY;