# Array of vectors

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

## 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=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 on other sites
The first one creates five instances of vector<float>.

##### 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 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 on other sites
Thank you all for your replies :)

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

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633684
• Total Posts
3013316
×