c++ - 将 int 数组初始化为类型参数

标签 c++ arrays

此代码编译失败:

  unordered_map<char, int[4]> inv = {
    { 'a', {{0,0,1,0}} } 
  }

当作为类型参数传递时,初始化此 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

最佳答案

这应该有效:

#include <array>

std::unordered_map<char, std::array<int, 4>> inv = {{ 'a', {{0, 0, 1, 0}} }};

关于c++ - 将 int 数组初始化为类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55138390/

相关文章:

c - 使用动态内存分配在二维数组中添加值

ios - Swift:在字典数组上使用过滤函数?错误:无法使用类型的参数列表调用 'filter'

c++ - 在 boost iostream filtering_ostream 中,sync()、strict_sync() 和 flush() 有什么区别?

c++ - 静态 C++ 变量 Apple Mach-o 链接器问题

C++模板T,检测Type为字符串形式

php - 为什么在 php 类中用作数组项不起作用

c++ - 使用复制构造函数创建对象(三类简单规则)会产生运行时错误

c++ - 在原子多线程代码中删除容器

php - PHP 数组中的数据库数据

python - for循环中不同大小数组的函数