Jump to content
  • Advertisement
Sign in to follow this  
mapPhil

Multiple Iterators or just one?

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

Hi,

I just want to know if it better to use different iterators for each for loop?

for example if I had a for loop to update the position of the sprites stored in a vector and another for loop to draw the sprites. Should I use the same iterator for both for loops or different?

I just want to know what the preferred way is.

Share this post


Link to post
Share on other sites
Advertisement
Personally I tend to declare the iterator in the for statement of each loop. In practice I found its fairly rare for me to iterate over the same type in a given function anyway.

e.g.
[source lang="cpp"]
void MyClass::doSomthing()
{
for(std::vector<int>::const_iterator i = numbers.begin(); i != numbers.end(); ++ i)
...

for(std::vector<Groups>::iterator i = groups.begin(); i != groups.end(); ++i)
{
for(std::vector<Object>::iterator j = i->begin(); j != i->end(); ++j)
...
}
}
[/source]
Of course const_iterator and iterator could be a number of other things from integers to pointers depending on what im doing.

The only time I declare the iterator outside the loop is when I wish to access it outside the loop for some reason, but that rarely comes up.

Share this post


Link to post
Share on other sites
Generally speaking scope the iterator inside the loop if possible. Re-using iterators would be like re-using loop counters, nothing technically wrong with it but counter intuitive and unnecessary.

One potential problem is illustrated with the following code:

i = list.begin();
while(i != list.end())
{
// use *i
++i;
}

// Later...

while(i != list.end())
{
// use *i
++i;
}

The second loop won't execute because we forgot to reset the iterator.

Share this post


Link to post
Share on other sites
I've been watching Herb Sutter's lambda talks recently. If you have a compiler with C++0x lambda support, he would suggest something along the lines of

[source lang="cpp"]
std::for_each(container.begin(), container.end(), [/* external variables to capture go here */](container::value_type value)
{
...
// do stuff
...
});
[/source]

You don't need to advance the iterator yourself, it is handled by std::for_each The code also has some self documenting qualities (you are stating that this is for each element). You can keep the code close to the loop, unlike how you had to use std::for_each before lambda support (defined elsewhere in the code).

Share this post


Link to post
Share on other sites
Something about optimizing prematurely and the anti-christ.

I'm not 100% sure about how loops translate into machine code with iterators, but for numerical counters there should be no benefit to re-declaring counters for new loops. I would assume that modern compilers would optimize them to be pretty much the same.

If you have two loops operating over the same set of iterators in the same function it seems like you could just turn them into one loop somehow and not have the issue at all.

Share this post


Link to post
Share on other sites

I've been watching Herb Sutter's lambda talks recently. If you have a compiler with C++0x lambda support, he would suggest something along the lines of

[source lang="cpp"]
std::for_each(container.begin(), container.end(), [/* external variables to capture go here */](container::value_type value)
{
...
// do stuff
...
});
[/source]

You don't need to advance the iterator yourself, it is handled by std::for_each The code also has some self documenting qualities (you are stating that this is for each element). You can keep the code close to the loop, unlike how you had to use std::for_each before lambda support (defined elsewhere in the code).


If you don't have C++0x lambda support yet, you can also use the BOOST_FOREACH macro.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!