如何在C++ 11中初始化(通过初始化列表)多维std :: array?

Mih*_*şog 13 c++ c++11

我试图通过初始化程序列表初始化2D std :: array但是编译器告诉我初始化程序太多了.

例如:

std::array<std::array<int, 2>, 2> shape = { {1, 1},
                                            {1, 1} };
Run Code Online (Sandbox Code Playgroud)

编译错误:错误:初始化程序太多 ‘std::array<std::array<int, 2ul>, 2ul>’

但显然没有太多.难道我做错了什么?

ken*_*ytm 13

尝试再添加一对{}以确保我们正在初始化内部C数组.

std::array<std::array<int, 2>, 2> shape = {{ {1, 1},
                                             {1, 1} }};
Run Code Online (Sandbox Code Playgroud)

或者只是删除所有括号.

std::array<std::array<int, 2>, 2> shape = { 1, 1,
                                            1, 1 };
Run Code Online (Sandbox Code Playgroud)


Bas*_*tch 6

我建议(甚至没有尝试过,所以我可能错了)

typedef std::array<int, 2> row;
std::array<row,2> shape = { row {1,1}, row {1,1} };
Run Code Online (Sandbox Code Playgroud)