Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multidimensional std::array [duplicate]

Tags:

c++

stl

In C++, how do I create a multidimensional std::array? I've tried this:

std::array<std::array<int, 3>, 3> arr = {{5, 8, 2}, {8, 3, 1}, {5, 3, 9}}; 

But it doesn't work. What am I doing wrong and how do I fix this?

like image 233
LazySloth13 Avatar asked Jul 20 '13 07:07

LazySloth13


People also ask

Can you clone a 2D array?

A simple solution is to use the clone() method to clone a 2-dimensional array in Java. The following solution uses a for loop to iterate over each row of the original array and then calls the clone() method to copy each row.

Are multidimensional arrays bad?

Multidimensioal arrays are more than double pointers making it a pain to pass into functions correctly. Most of the time, I've seen them just passed as a raw address to a double pointer which defeats the intrinsic math the compiler will do for you.


1 Answers

You need extra brackets, until c++14 proposal kicks in.

std::array<std::array<int, 3>, 3> arr = {{{5, 8, 2}, {8, 3, 1}, {5, 3, 9}}}; 
like image 97
billz Avatar answered Sep 18 '22 00:09

billz