# Array or vector of bullet objects?

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

## Recommended Posts

Hi, can anyone weight up the pro's and con's of using an array of bullets rather than a vector and vice versa? Basically, I can set a bool to false once a bullet collides with another game entity, then that bullet will become unusable until the player collides with more ammunition in the world map, then set the bool back to true. Or, just keep track of how many ammunition packs the player collides with then i.e. IF AmmoCount != 0 && Trigger Pressed m_BulletVec.push_back(new Bullet()) END IF Thanks!:)

##### Share on other sites
Quote:
 Original post by DrunkenBritHi, can anyone weight up the pro's and con's of using an array of bullets rather than a vector and vice versa?Basically, I can set a bool to false once a bullet collides with another game entity, then that bullet will become unusable until the player collides with more ammunition in the world map, then set the bool back to true.Or, just keep track of how many ammunition packs the player collides with then i.e.IF AmmoCount != 0 && Trigger Pressed m_BulletVec.push_back(new Bullet())END IF
You could do both: re-use an existing inactive bullet if one is available, else add a new one to the vector. That way you won't have to add new objects to the vector very often, but at the same time the number of bullets will be unlimited (practically speaking).

Thanks man :)

##### Share on other sites
Quote:
 Original post by jykYou could do both: re-use an existing inactive bullet if one is available, else add a new one to the vector. That way you won't have to add new objects to the vector very often, but at the same time the number of bullets will be unlimited (practically speaking).

It means that vector insertion becomes O(n), which may not be very practicable. I suggest using a std::list (because of its amortized constant time push_back()). Erasing an element also (amortized) constant time. Moreover, it is very rare to need a random access to a list of bullets, thus you won't need this nifty operator[] (most of the time, you'll use it in a loop; the same loop can be written using iterators, so you don't loose anything).

HTH,

##### Share on other sites
Okay, what you just said went right over my head :) But i'll do some research, thanks :)

##### Share on other sites
Quote:
 Original post by DrunkenBritOkay, what you just said went right over my head :) But i'll do some research, thanks :)

Oh ! Sorry [embarrass]. I try to reword everything.

Instead of using a std::vector<> or a plain C array, I suggest you to use a std::list<>, for two main reasons:
• the insertion is rather fast
• the deletion is rather fast too

Of course, it is a list, meaning that you waon't be able to access a single item using the bracket [] operator (container won't work). Anyway, it should not be a problem because most of the time you only use it in a loop
for (size_t i=0; i<array.size(); i++) {  data = array;}

This loop can be reworked to use iterators instead:
for (std::list<MyType>::iterator it = list.begin(); it != list.end(); ++it) {  data = *it;}

Of course, if you use the [] operator outside loops then using a std::list may not be a good solution.

I hope it is clearer :)

##### Share on other sites
I get ya, thanks :)

1. 1
Rutin
19
2. 2
3. 3
JoeJ
16
4. 4
5. 5

• 26
• 20
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001783
×