-4

The following is an example of a multi-dimensional array declaration in C#:

var Number = new double[2, 3, 5]
    {
        {
             {  12.44, 525.38,  -6.28,  2448.32, 632.04 },
             {-378.05,  48.14, 634.18,   762.48,  83.02 },
             {  64.92,  -7.44,  86.74,  -534.60, 386.73 }
        },
        {
             {  48.02, 120.44,   38.62,  526.82, 1704.62 },
             {  56.85, 105.48,  363.31,  172.62,  128.48 },
             {  906.68, 47.12, -166.07, 4444.26,  408.62 }
        },
    };

I would like to do the same in C++, but do not know how and have not read my C++ book.

How can I accomplish this?

Community
  • 1
  • 1
user522745
  • 161
  • 1
  • 2
  • 7

2 Answers2

1

Here is a naive way of doing that without explicitly specifying the bounds:

vector<vector<vector<double>>> v =
{
    {
        {  12.44, 525.38,  -6.28,  2448.32, 632.04 },
        {-378.05,  48.14, 634.18,   762.48,  83.02 },
        {  64.92,  -7.44,  86.74,  -534.60, 386.73 }
    },
    {
        {  48.02, 120.44,   38.62,  526.82, 1704.62 },
        {  56.85, 105.48,  363.31,  172.62,  128.48 },
        {  906.68, 47.12, -166.07, 4444.26,  408.62 }
    },
};

However, the vector of vectors will allow you to push elements into each dimension freely (they are just independent vectors), so you might end up not having a consistent multi-dimensional vector. In particular, the compiler won't detect any attempt to put more or less elements into one dimension than into another one.

If that is a concern, you might want to check Boost.MultiArray.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
0

You don't need the standard library, or Boost, to define and initialize a fixed size multidimensional array.

double Number[2][3][5] =
    {   
        {   
             {  12.44, 525.38,  -6.28,  2448.32, 632.04 },
             {-378.05,  48.14, 634.18,   762.48,  83.02 },
             {  64.92,  -7.44,  86.74,  -534.60, 386.73 }
        },  
        {   
             {  48.02, 120.44,   38.62,  526.82, 1704.62 },
             {  56.85, 105.48,  363.31,  172.62,  128.48 },
             {  906.68, 47.12, -166.07, 4444.26,  408.62 }
        },  
    };
David Hammen
  • 32,454
  • 9
  • 60
  • 108