Sign in to follow this  

Array of vectors

This topic is 4304 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 need to build an array of vectors and I did as follow: std::vector<float> myArray[5]; Is this way of doing ok or do I need to construct each vector individually like: std::vector<float>* myArray[5]; for (int i=0;i<5;i++) myArray[i]=new std::vector<float>; I have been using the first way of doing but get some weird effect sometimes. Or to put the question in another way: does the first instruction create 5 instances of vectors or just reserves the space for them? Can someone enlighten me? Thanks

Share this post


Link to post
Share on other sites
On the first one, yeah, you're creating 5 instances of vectors. The proper way to do that would be like so:

std::vector<float> myArray(5); // initialize array to default size of 5

And for the second one...

std::vector<float>* myArray = new std::vector<float>(5); // initialize pointer array to default size of 5

// and then initialize each individually...

Hope that helps. :)

Share this post


Link to post
Share on other sites
I think he wants 5 vectors of floats, not a vector of 5 floats. In which case, his original way is correct, although std::vector<std::vector<float> > arr(5) would also work, but the resize complexity would be horrible.

Share this post


Link to post
Share on other sites
ZQJ (or anyone), can you elaborate on the efficiency of resizing the 2D array of vectors? I've been skeptical about using vectors in the past, for anything more than 1D arrays, for speed reasons.


discman1028

[Edited by - discman1028 on March 2, 2006 12:38:05 AM]

Share this post


Link to post
Share on other sites

This topic is 4304 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