Jump to content
  • Advertisement
Sign in to follow this  
Decrius

Nested C++ vectors

This topic is 3821 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've read that having a vector in a vector is a bad idea, and deque's should be used, but I'm unsure if that applies to this case aswell. I have containers and components. Each container has a vector of (sub-)containers and components, each (sub-)containers aswell, aswell as those (sub-sub-)containers. Both Container and Component are classes, where container has 2 vectors, one for containers, one for components. So in fact it's like this: class -> vector -> class -> vector -> class -> vector ... Should I use deques instead of vectors for the containers? ---- And, sub question: to get rid of the complicated 2 vectors functions to see it as one vector, I thought of making a component a derived class of a container. But Components can be used as base class for new components. Can I do that? Container would then be the base class (with derived classes aswell). I would then derive Component off Container, so they both fit in one vector (true aye?). Then for every variation on Component I make a derived class off Component. Would this be a good idea? Or should I leave the (rather long) code I have written to solve this problem the other way?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Decrius
Hi,

I've read that having a vector in a vector is a bad idea, and deque's should be used, but I'm unsure if that applies to this case aswell.
Whaaa? vectors-of-vectors and deques have nothing to do with each other. A vector of vectors is generally a bad choice for representing a 2D grid (a better choice is a single vector with row- or column-packing), but that doesn't apply here.

Quote:
Should I use deques instead of vectors for the containers?
Only if you need efficient insertion and removal from the front of the container. That's the only significant difference between vector and deque.

Quote:
And, sub question: to get rid of the complicated 2 vectors functions to see it as one vector, I thought of making a component a derived class of a container. But Components can be used as base class for new components. Can I do that?

You're rediscovering the Composite Pattern. Read about that.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Whaaa? vectors-of-vectors and deques have nothing to do with each other. A vector of vectors is generally a bad choice for representing a 2D grid (a better choice is a single vector with row- or column-packing), but that doesn't apply here.


No, I didn't mean a vector in a vector is like a deque. Deque allocates new memory, while a vector has it all in one allocation, isn't it? Deques have multiple blocks for storage.

Quote:
Original post by Sneftel
Only if you need efficient insertion and removal from the front of the container. That's the only significant difference between vector and deque.


They get added once, are read multiple times and are then (automatically) deallocated. Even their sequence does not matter as a 'layer' vector is used for holding the 'height' of a vector.

Quote:
Original post by Sneftel
You're rediscovering the Composite Pattern. Read about that.


Hmm :(, too bad I made it already. I'll read about it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Decrius
No, I didn't mean a vector in a vector is like a deque. Deque allocates new memory, while a vector has it all in one allocation, isn't it? Deques have multiple blocks for storage.

Perhaps, depending on the implementation. But the outside interface is the same. A deque does not give you direct access to multiple blocks.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Quote:
Original post by Decrius
No, I didn't mean a vector in a vector is like a deque. Deque allocates new memory, while a vector has it all in one allocation, isn't it? Deques have multiple blocks for storage.

Perhaps, depending on the implementation. But the outside interface is the same. A deque does not give you direct access to multiple blocks.


And it especially doesn't give you control over the block size.

Share this post


Link to post
Share on other sites
Nope, I'd like to handle it like the vector. I was wondering if it would be better to use the deque in this case, if it could speed things up...

Share this post


Link to post
Share on other sites
Choosing between a deque and vector is a tricky task - but seeing as their interfaces are very very similar, it's easy to switch back and forth.

Generally:
* If you need to access the elements as a c-style array, use a vector
* If you know the size the container needs to be, use a vector (with reserve)
* Else use a deque and switch to a vector if profiling tells you that the deque is causing a bottleneck.

Quote:

if it could speed things up...

I think a deque may be able to speed your code up because copying the elements stored in the container looks like an expensive operation - and deques don't use the grow/copy strategy that vectors do.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!