Nested C++ vectors

Started by
5 comments, last by Nitage 15 years, 10 months ago
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?
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
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.
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.

[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
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.
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.
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...
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
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.

This topic is closed to new replies.

Advertisement