Lan*_*don 1 c++ enums stl c++11
我需要一种将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;
}
Run Code Online (Sandbox Code Playgroud)
有没有一种从一个枚举类映射到另一个枚举类的方法?
我想要类似的东西
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)
Run Code Online (Sandbox Code Playgroud)
感谢您的时间 :)
编辑1:修复了丢失的逗号
编辑2:添加了我得到的编译错误
编辑3:添加了我正在尝试的完整资源
编辑4:没有新的代码或任何东西,只是为了误解这些帖子如何工作而向所有人道歉.我仍然很难理解编译错误,而且我没有找到我在谷歌中寻找的确切内容.我认为可能更容易直接询问我想要什么,也许可以解释类枚举如何使用地图工作(或不工作).我将继续使用我的测试源并继续关注Google.对不起因为你们所有人的烦恼:(
编辑5:
你们很友好地告诉我我的错误在哪里,谢谢.我没有看到它sdt::map的insert功能没有把关键和值作为参数.起初我认为错误意味着没有std::map命名插入存在,当我检查它是否在线时,我不知何故错过了查看参数,确信这与某些事情有关enum classes,因为我之前从未使用它们.
根据http://en.cppreference.com/w/cpp/container/map,std :: map是一个有序关联容器,包含具有唯一键的键值对.因此,您必须插入一对.
更换
lectureToDayMap.insert(Foobar::Lecture::COMP_SCI, Foobar::Day::MONDAY);
Run Code Online (Sandbox Code Playgroud)
通过
mymap.insert(std::make_pair(Foobar::Lecture::COMP_SCI, Foobar::Day::MONDAY));
Run Code Online (Sandbox Code Playgroud)
或更好
lectureToDayMap[Foobar::Lecture::COMP_SCI] = Foobar::Day::MONDAY;
Run Code Online (Sandbox Code Playgroud)