如何在c ++ 11 / c ++ 14中定义一个带有枚举的枚举和值作为整数的地图? [英] How to define a map with key as a enum and value as an integer in c++11/c++14?
问题描述
枚举符号
{
BAR,
BELL,
PLUM,
ORANGE,
CHERRY,
DOESNOTMATTER,
} wheel1,wheel2,wheel3;
map< Symbols [3],int> symb = {{{BAR,BAR,BAR},250},
{{BELL,BELL,BELL},20},
{{BELL,BELL,BAR},20},
{{PLUM,PLUM,BAR},14},
{{PLUM,PLUM,PLUM},14},
{{ORANGE,ORANGE,BAR},10},
{ {ORANGE,ORANGE,ORANGE},10},
{{CHERRY,CHERRY,CHERRY},7},
{{CHERRY,CHERRY,DOESNOTMATTER},5},
{{CHERRY ,DOESNOTMATTER,DOESNOTMATTER},2}};
我已经定义了一个名为Symbols的枚举器。我正在尝试创建一个地图,关键是一个符号枚举数组,值是一个整数。
当我尝试运行这个程序时,我得到一个错误 strong> /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c ++ / v1 / utility:268:11:数组初始化器必须是初始化列表
我在这里做错了什么?我也尝试插入这些值。错误是一样的。
原始数组 Symbols [3]
与容器的要求不符合 的关键类型std :: map
。
std :: map满足Container,AllocatorAwareContainer, AssociativeContainer和ReversibleContainer。
请参阅 C ++概念:容器
CopyInsertable
EqualityComparable
可破坏
您可以使用 std :: array
或 std :: vector
。例如: std :: map< std :: array< Symbols,3>,int> symb = ...
enum Symbols
{
BAR,
BELL,
PLUM,
ORANGE,
CHERRY,
DOESNOTMATTER,
}wheel1, wheel2, wheel3;
map<Symbols[3], int> symb = {{{BAR, BAR, BAR}, 250},
{{BELL, BELL, BELL}, 20},
{{BELL, BELL, BAR}, 20},
{{PLUM, PLUM, BAR}, 14},
{{PLUM, PLUM, PLUM}, 14},
{{ORANGE, ORANGE, BAR}, 10},
{{ORANGE, ORANGE, ORANGE}, 10},
{{CHERRY, CHERRY, CHERRY}, 7},
{{CHERRY, CHERRY, DOESNOTMATTER}, 5},
{{CHERRY, DOESNOTMATTER, DOESNOTMATTER}, 2}};
I have defined an enumerator named Symbols. I am trying to create a map, who's key is a Symbols enum array and value is an integer.
When I try to run this program, I get an error "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/utility:268:11: Array initializer must be an initialiser list".
What am I doing wrong here? I have tried inserting the values too. The error is the same.
解决方案 The raw array Symbols[3]
doesn't match the requirement of the containers as a key type of std::map
.
std::map meets the requirements of Container, AllocatorAwareContainer, AssociativeContainer and ReversibleContainer.
CopyInsertable
EqualityComparable
Destructible
You can use std::array
or std::vector
. Such as:
std::map<std::array<Symbols, 3>, int> symb = ...
这篇关于如何在c ++ 11 / c ++ 14中定义一个带有枚举的枚举和值作为整数的地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!