Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a vector of int 2D array in c++ [duplicate]

i am trying to create a vector of int 2D array in c++ but my following code has some errors that i can't realize why?!

vector< int[2][2] > vec;
int a[2][2];
vec.push_back(a);

i don't want to use int ** int my vetor (and create a 2D array using new)

and i know each of 2D arrays has just 2 col and row (the size is static)

so is there any way to implement that vector or not?

and i also try to push_back just an array to vecotr and it wasn't successful too! why?

vector< int[2] > vec;
int a[2];
vec.push_back(a);

thanks in advance

like image 963
strings95 Avatar asked Dec 11 '25 03:12

strings95


1 Answers

Native arrays are not copyable, moveable or assignable, so they cannot be stored in a standard container.

However, the wrapper std::array<T,N> is, so std::vector<std::array<std::array<int, 2>, 2> > is one way to do what you want.

like image 191
Lightness Races in Orbit Avatar answered Dec 13 '25 17:12

Lightness Races in Orbit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!