Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does creating a 2D array with C++ std::array require an extra pair of {} [duplicate]

Possible Duplicate:
c++ why initializer_list behavior for std::vector and std::array are different

I defined simple 2D array (3X2):

  std::array<std::array<int,3>,2> a {
    {1,2,3},
    {4,5,6}
  };

I was surprised this initialization does not work, with gcc4.5 error: too many initializers for 'std::array<std::array<int, 3u>, 2u>'

Why can't I use this syntax?

I found workarounds, one very funny with extra braces, but just wonder why the first, easiest approach is not valid?

Workarounds:

  // EXTRA BRACES
  std::array<std::array<int,3>,2> a {{
    {1,2,3},
    {4,5,6}
  }};

  // EXPLICIT CASTING
  std::array<std::array<int,3>,2> a {
    std::array<int,3>{1,2,3},
    std::array<int,3>{4,5,6}
  };

[UPDATE]

Ok, thanks to KerrekSB and comments I get the difference. So it seems that there is too little braces in my example, like in this C example:

struct B {
  int array[3];
};
struct A {
  B array[2];
};

B b = {{1,2,3}};
A a = {{
     {{1,2,3}},
     {{4,5,6}}
}};
like image 782
PiotrNycz Avatar asked Oct 11 '12 16:10

PiotrNycz


People also ask

Why do we use two for loops when processing two-dimensional arrays?

For a two-dimensional array, in order to reference every element, we must use two nested loops. This gives us a counter variable for every column and every row in the matrix.

Why do we use 2D array in C?

The 2D array is organized as matrices which can be represented as the collection of rows and columns. However, 2D arrays are created to implement a relational database lookalike data structure. It provides ease of holding the bulk of data at once which can be passed to any number of functions wherever required.

Why do we need two-dimensional arrays?

The Need For Two-Dimensional Arrays Using 2d arrays, you can store so much data at one moment, which can be passed at any number of functions whenever required.

Why do we need to specify the number of columns when passing a two-dimensional array as a parameter to a function?

When passing a two-dimensional array to a function, you must specify the number of columns as a constant when you write the parameter type, so the compiler can pre-calculate the memory addresses of individual elements.


1 Answers

std::array<T, N> is an aggregate that contains a C array. To initialize it, you need outer braces for the class itself and inner braces for the C array:

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

Applying this logic to a 2D array gives this:

std::array<std::array<int, 3>, 2> a2 { { { {1, 2, 3} }, { { 4, 5, 6} } } };
//                                   ^ ^ ^ ^            ^ ^
//                                   | | | |            | |
//                                   | +-|-+------------|-+
//                                   +-|-+-|------------+---- C++ class braces
//                                     |   |
//                                     +---+--- member C array braces
like image 152
Kerrek SB Avatar answered Oct 24 '22 00:10

Kerrek SB