How do you initialize (through initializer lists) a multidimensional std::array in C++11?

后端 未结 2 1244
我寻月下人不归
我寻月下人不归 2020-12-16 14:57

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

e.g.:

std::ar         


        
相关标签:
2条回答
  • 2020-12-16 15:35

    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} }};
    

    Or just drop all the brackets.

    std::array<std::array<int, 2>, 2> shape = { 1, 1,
                                                1, 1 };
    
    0 讨论(0)
  • 2020-12-16 15:49

    I would suggest (without even have trying it, so I could be wrong)

    typedef std::array<int, 2> row;
    std::array<row,2> shape = { row {1,1}, row {1,1} };
    
    0 讨论(0)
提交回复
热议问题