如何初始化(通过初始化列表)C ++ 11中的多维std ::数组? [英] How do you initialize (trough initializer lists) a multidimensional std::array in C++11?

查看:144
本文介绍了如何初始化(通过初始化列表)C ++ 11中的多维std ::数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过初始化器列表初始化一个2D std ::数组,但是编译器告诉我有太多的初始化器。

I am trying to initialize a 2D std::array trough initializer lists however the compiler tells me that there are too many initializers.

例如:

std::array<std::array<int, 2>, 2> shape = { {1, 1},
                                            {1, 1} };

编译器错误:错误:'std :: array& std :: array< int,2ul> ;, 2ul>'

但是显然没有太多。

推荐答案

尝试再添加一个 {} 以确保我们初始化内部C数组。

Try to add one more pair {} to ensure we're initializing the internal C array.

std::array<std::array<int, 2>, 2> shape = {{ {1, 1},
                                             {1, 1} }};

或者删除所有括号。

std::array<std::array<int, 2>, 2> shape = { 1, 1,
                                            1, 1 };

这篇关于如何初始化(通过初始化列表)C ++ 11中的多维std ::数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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