Any algorithm to find value of a datamember from a class in a vector?

Started by
3 comments, last by linternet 16 years, 1 month ago
I have a vector of sprites, each sprite has a pointer to a body (that is handled by the physics engine). I have done a conditional loop using the physics engine, to decide if i should destroy the body, so i have an iterator to the body, but i need to also find the sprite with the pointer to this body. Is there an algorithm for doing that? i was thinking of find() at first. I know that each sprite has its uniqe body.
•°*”˜˜”*°•.˜”*°•..•°*”˜.•°*”˜˜”*°•..•°*”˜˜”*°•.˜”*°•.˜”*°•. Mads .•°*”˜.•°*”˜.•°*”˜˜”*°•.˜”*°•..•°*”˜.•°*”˜.•°*”˜ ˜”*°•.˜”*°•.˜”*°•..•°*”˜I am going to live forever... or die trying!
Advertisement
Could you give the body a pointer to its sprite?
Nick Wilson - Junior C# Developer | See my crappy site
A back pointer would work. Sending a signal to the sprite would also work, how does the physics engine update the sprite when something changes?
Ahh, a back pointer :D thanks guys!
•°*”˜˜”*°•.˜”*°•..•°*”˜.•°*”˜˜”*°•..•°*”˜˜”*°•.˜”*°•.˜”*°•. Mads .•°*”˜.•°*”˜.•°*”˜˜”*°•.˜”*°•..•°*”˜.•°*”˜.•°*”˜ ˜”*°•.˜”*°•.˜”*°•..•°*”˜I am going to live forever... or die trying!
If I understand you correctly, you're probably best off, in this case, using the address of the iterator to the body (ie: &(*iter) ) and manually looping through your sprite vector looking for that value.

I believe (and someone is sure to correct me if I'm wrong :) ) that find is going to look for values of the vector's type. If the sprite IS the pointer to the body then you can use the aforementioned address and the find algorithm to search your vector for that value.

I would guess, however, that your sprite is a more complicated object. In that case find isn't going to work with the pointer to your body unless you override the == operator on your sprite class to comapre a sprite object with a body pointer (which might be more work than it's worth).


This topic is closed to new replies.

Advertisement