• Advertisement
Sign in to follow this  

Looking for something in a loop

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

This is a trivial question, driven by curiosity alone. When you're looping to check for the existence of something, how do you prefer to "remember" that you found one, once you're outside of the loop? For example, if I wanted to make sure a suit contained the right shape to fit a certain person when that person is looking through them, I don't need to keep any records, other than that the suit supports that person's shape. This is how I could do it:
BOOL found_shape = false;
for(int m=0; m<Shapes.GetCount(); m++)
{
	if( Shapes[m] == Person->GetShape() )
	{
		found_shape = true;
		break;
	}
}

Here's another way I could have done it:
int m;
for(m=0; m<Shapes.GetCount(); m++)
	if( Shapes[m] == Person->GetShape() )
		break;
BOOL found_shape = ( m < Shapes.GetCount() );


Is that basically how everyone does it? Or are there any other ideas?

Share this post


Link to post
Share on other sites
Advertisement
std::set<Shape*> shapes;

std::set<Shape*>::const_iterator i = shapes.find(Person->GetShape());
if (i == shapes.end()) {
// not found
} else {
// found
}

Share this post


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

  • Advertisement