1

This is the first time that I am going to use a multidimensional std::array. I want to declare 9 matrix of 3 x 3 dimensions.

In a conventional c++ array we declare like the following.

int a[9][3][3];

When I try to declare this array using std::array the sequence of dimensions is upside down, It must be declared like the following.

array<array<array<int, 3>, 3, 9)

my expectation was to declare that matrix like we declare the conventional arrays like the following.

  array<array<array<int, 9>, 3, 3)

There is no documentation on this matter. What is the C++ standard

  • 2
    *"I want to declare 9 matrix of 3 x 3 dimensions."* Then the first way you wrote it is correct, not the last way (except you're missing some `>` for your template params) – Cory Kramer Jul 14 '18 at 15:07
  • `array, 3, 9)` This works? Shouldn't it be `array, 3>, 9>`? – kiner_shah Jul 14 '18 at 15:08
  • 4
    What about doing it in two steps? First `using matrix = array, 3>;` and then `array`. – Bo Persson Jul 14 '18 at 15:09
  • Each `<` needs to be matched with a `>`. So it is not `array, 3, 3)` (like you have), it is `array, 3>, 3>`. Depending on order of indices you need (you haven't specified), you may need to adjust to `array, 3>, 9>` – Peter Jul 14 '18 at 15:12
  • What is there to document? An `array` is a length N array of `T`. That is all you need to know. – juanchopanza Jul 14 '18 at 15:40

2 Answers2

4

Use > instead of ) :

using matrices = array<array<array<int,3>,3>,9>;

And you can write, as suggested in the comments:

// N is number of rows, M number of columns
template<int N, int M>
using matrix = array<array<int,N>,M>;

using matrices = array<matrix<3,3>,9>;
Olivier Sohn
  • 1,292
  • 8
  • 18
0

Check out Why can't simple initialize (with braces) 2D std::array?

Try std::array<std::array<std::array<int, 3>, 3>, 9> myArray;.

Remember to #include<array>.