Sign in to follow this  

Vectors of arrays

This topic is 4824 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

This topic is 4824 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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