Sign in to follow this  

Question about STL

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

The way im using STL's is like this. struct example { int x; }; example *mystruct; vector<example*> myvect; Then I make one so I go: mystruct= new example; mystruct->x=0; myvect.pushback(mystruct); So whenever I want to loop through I just go. for (index=0; index < myvect.size(); index++) { myvect[index]->x++; } That is about as easy as STL's get. My question is, is there a way I can double up on my vectors. in other words, how would my dynamic structure, contain it's own dynamic variables/structure. unit[index]->unit2[index2] ? Or something similure. The only reason for this is when I create my main unit, A space ship, and it has 20 turrets. When the ship blows up, all the ships turrets will need to be killed as well. But the idea here is I can have 10 ships all with their own 20 turrets. Now I could keep my turrets in it's own STL, and have a owner flag= the index of the ship STL. Only problem with that is if I erase a ship, I have to go through the turret STL, and tell it that it's new owner is index-1. That would be of course if the first ship in the index was erased. If I kill the last ship in the STL index, and I tell it to kill the turrets of the owner, it would work fine. Wouldn't have to re-index. The idea here is to keep everything dynamic. I could just use arrays, but then I feel like im wasting memory. I'm open to any suggestions, I don't even know if what I want to do is possible. Or if anyone has a better system when 1 STL is based off the existence of another STL.

Share this post


Link to post
Share on other sites
There's nothing saying you can't have something like this:


struct ship {
int x, y;
vector <turret *> turrets;
}

struct turret {
// Whatever turrets have in them these days
}

...

vector <ship *> ships;


You just have to make sure that you initialize the turrets properly and that you clean up after yourself.

If you mean more simply a vector of vectors (no containing class), then you can do this: vector < vector <ship> > ships2DArray; Note the extra spaces. They're needed to differentiate from the right shift operator.

-Auron

Share this post


Link to post
Share on other sites

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