Vectors of arrays

Started by
5 comments, last by iMalc 19 years, 6 months ago
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.
Advertisement
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
Ta very much
[off-topic]

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

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


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?

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement