The initialization of a multidimensional
std::array
is indeed a bit a surprise: you need to add an extra level of braces. E.g.
#include <array>
...
using namespace std;
...
enum { OUTER = 3, INNER = 2 };
array<array<int, INNER>, OUTER> a = { {{ 1, 2 }, { 3, 4 }, { 5, 6 }} } ;
for(size_t outer = 0; outer < OUTER; ++outer) {
for(size_t inner = 0; inner < INNER; ++inner) {
cout << "a[" << outer << "][" << inner << "] = " << a[outer][inner] << endl;
}
}
Result:
a[0][0] = 1
a[0][1] = 2
a[1][0] = 3
a[1][1] = 4
a[2][0] = 5
a[2][1] = 6
You may leave away all but the outer braces and initialize the array as flat sequence of elements.
See also
http://stackoverflow.com/questions/17759757/multidimensional-stdarray[
^] and the referenced
C++14 proposal[
^] to fix that "defect".
For dynamic "arrays" you may use nested
std::vector
s.
Regards
Andi