初始化一个int数组作为类型参数

duk*_*vin 0 c++ arrays

此代码无法编译:

  unordered_map<char, int[4]> inv = {
    { 'a', {{0,0,1,0}} } 
  }
Run Code Online (Sandbox Code Playgroud)

作为类型参数传递时,初始化此int数组的正确方法是什么?

我试过了:int[]array<int,4>但是它们都给出了错误:

no instance of constructor "std::unordered_map<_Kty, _Ty, _Hasher,
_Keyeq, _Alloc>::unordered_map [with _Kty=char, _Ty=std::pair<Cell *, int []>, _Hasher=std::hash<char>, _Keyeq=std::equal_to<char>,
 _Alloc=std::allocator<std::pair<const char, std::pair<Cell *, int []>>>]" matches the argument list
Run Code Online (Sandbox Code Playgroud)

lub*_*bgr 5

这应该工作:

#include <array>

std::unordered_map<char, std::array<int, 4>> inv = {{ 'a', {{0, 0, 1, 0}} }};
Run Code Online (Sandbox Code Playgroud)

  • 您正在使用哪个编译器?适用于`clang 7`和`gcc 8`。 (2认同)
  • @LarsNielsen`std :: unordered_map &lt;char,int [4]&gt; myMap;`将会编译,但是任何插入都将编译失败,因为您无法分配给现有的`int [4]`数组,例如`myMap [ 'a'] = {1,2,3,4};`给您类似“ int [4]不可分配”的信息。与“ int test [4] = {1、2、3、4};”(有效)相同,然后与“ test = {5、6、7、8}”相同。 (2认同)