Sign in to follow this  
JimPrice

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 this post


Link to post
Share on other sites
snk_kid    1312
Quote:
Original post by JimPrice

If I have a vector of arrays of ints, how do I add an element:

vector<int[2]> 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

Share this post


Link to post
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 this post


Link to post
Share on other sites
capn_midnight    1707
Quote:
Original post by Nietsnie
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?
the problem is, once you retrieve that pointer from the vector, how large is your array?

Share this post


Link to post
Share on other sites
iMalc    2466
Quote:
Original post by Nietsnie
He 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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this