我可以使用std::map,其中键和值都是c++ 11中的枚举类吗?
Can I use an std::map with both keys and values being enum classes in 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;
}
是否有从一个枚举类映射到另一个枚举类的方法?
我想要像
这样的东西 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::map
的insert
函数没有将键和值作为参数。起初,我认为这个错误意味着没有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;
相关文章:
- 不带大括号的枚举形式
- 枚举环境变量的惯用C++14/C++17方法
- 类似枚举的计算常量
- 如何正确实现和访问运算符的各种自定义枚举器
- 错误:从"int"到枚举c++的转换无效
- C++中构造函数中的枚举
- 访问在 C++ 结构中声明的枚举变量
- 枚举类'classname'的多重定义
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- typedef 枚举和枚举类有什么区别?
- 为什么我的开关/机箱在使用枚举时默认?
- 标准::可选枚举的比较运算符
- C++两个源文件之间共享的枚举的静态实例
- 打印没有铸件的枚举可以在C++中吗?
- 枚举成员与静态 int 成员?
- C++:枚举:错误:应使用标识符而不是"}"
- 带有 c++ 的枚举(输入检查)
- 在 qml 中使用 Q_ENUM 和 Q_PROPERTY 作为枚举类
- 为什么 int 类型的枚举类值不能用作 int
- 在 C++ 中输出枚举类类型的向量元素