简体   繁体   中英

How to declare std::vector with an 'n' dimensional array?

My thoughts are the following:
for eg a two-dimensional array:

int a[9][9];
std::vector<int** a> b;

But what if I have

/* I know, it is usually a bad practise to have more than 2 dimensional arrays, but it can happen, when you need it */
int a[3][4][5][6]; 
std::vector<int**** a> b; // ? this just looks bad

Try this:

struct MD_array{ //multidimentional array
   a[3][4][5][6];
};
std::vector<MD_array> b;

Then you can access each array like so:

b[i].a[x][y][z][w] = value;

You can do this

int a[3][4][5][6]; 
std::vector<int**** a> b; 

in two ways like this

int a[3][4][5][6]; 
std::vector<int ( * )[4][5][6]> b; 

b.push_back( a );

and like this

int a[3][4][5][6]; 
std::vector<int ( * )[3][4][5][6]> b; 

b.push_back( &a );

Though it is not clear what you are trying to achieve.:)

You could also use an alias declaration:

template <typename T, size_t I, size_t J, size_t K, size_t N>
using SomeArr = std::array<std::array<std::array<std::array<T, I>, J>, K>, N>;

int main()
{
    SomeArr<int,3,4,5,6> arr;
    std::vector<SomeArr<int,3,4,5,6>> someVec;
    someVec.push_back(arr);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM