# Vectors of arrays

## Recommended Posts

JimPrice    815
Hi, I'm obviously missing something obvious, but how does one go about doing this: If I have a vector of ints, for example, I can use vector<int> myVec; myVec.push_back(1); If I have a vector of arrays of ints, how do I add an element: vector<int[2]> myVec; myVec.push_back(??) Thanks, Jim.

##### Share on other sites
snk_kid    1312
Quote:
 Original post by JimPriceIf I have a vector of arrays of ints, how do I add an element:vector myVec;myVec.push_back(??)

Its not possible, the closest you'll get to that with vector is something like this:

typedef std::vector<int> vec_of_ints;typedef std::vector< vec_of_ints > vec2d_of_ints;vec2d_of_ints::size_type cols = /* some-size */vec_of_ints::size_type rows = /* some-size */vec2d_of_ints v(cols, vec_of_ints(rows, 0));

there are other ways of constructing it but you can't have a vector of C-style arrays only vector of vectors (or vector of some other container).

but its not really a nice way to handle multidimensional arrays, you might wont to consider this

JimPrice    815
Ta very much

##### Share on other sites
demonkoryu    980
[off-topic]

snk_kid, you got a 1337 rating [smile]
I don't dare to rate you up now :)

Thermo

##### Share on other sites
Nietsnie    270
He is 1337! :)

Could you not just use pointers? Such, creat an array and use a pointer to that array as the datatype for the vector? Would that work, or I am completely wrong?

##### Share on other sites
capn_midnight    1707
Quote:
 Original post by NietsnieHe is 1337! :)Could you not just use pointers? Such, creat an array and use a pointer to that array as the datatype for the vector? Would that work, or I am completely wrong?
the problem is, once you retrieve that pointer from the vector, how large is your array?

##### Share on other sites
iMalc    2466
Quote:
 Original post by NietsnieHe is 1337! :)
Actually he's more than 1337 now (1341). He's like ultra 1337.

In fact if they were after more admins, snk would be the man I'd recommend for the job.