将int数组初始化为类型参数
Initializing an int array as a type argument
此代码无法编译:
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}} }};
您可以用一组角括号初始化数组。
int main()
{
std::unordered_map<char,std::array<int,4>> inv = {{ 'a', {1,2,3,4} }};
for (auto &&i : inv)
std::cout<< i.first << "->" <<i.second[0] <<std::endl;
}
示例:https://rextester.com/fovmlc70132
相关文章:
- C++转换参数初始化问题
- 在构造函数中使用可变参数初始化 std::tuple
- 如何使用派生类类型数据初始化 std::shared_ptr?
- 类模板专用化演绎是否应该考虑演绎指南参数初始化?
- OOP - 抽象类类型,初始化基类和派生类中的变量
- 使用类型参数专用化部分模板参数(VC++ v140 工具集)
- 如何通过参数初始化构造函数中的数组?
- 在模板类中使用"this"会导致参数初始化错误
- 如何使用一些参数初始化动态分配的数组中的对象
- 使用模板参数初始化静态 const 成员
- 基于模板参数初始化静态字符
- 使用参数初始化
- 使用参数包参数初始化"std::array"
- 从按值构造函数参数初始化的引用成员
- 字符串数据类型变量初始化
- 构造函数初始化列表中POD类型的初始化
- MSVC 19.11 / Visual C 2017:尺寸1和size_t类型的初始化列表误解
- 是其他类型的不同类型的初始化衍生物,或者它们是完全分开的
- 使用派生的[C++]生成的参数初始化基类
- 使用结构非类型可变参数模板参数初始化结构的字符串数据成员