如何在c ++ 11 / c ++ 14中定义一个带有枚举的枚举和值作为整数的地图? [英] How to define a map with key as a enum and value as an integer in c++11/c++14?

查看:174
本文介绍了如何在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 = ... 

LIVE


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.

See C++ concepts: Container

CopyInsertable
EqualityComparable
Destructible 

You can use std::array or std::vector. Such as:

std::map<std::array<Symbols, 3>, int> symb = ...

LIVE

这篇关于如何在c ++ 11 / c ++ 14中定义一个带有枚举的枚举和值作为整数的地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆