Jump to content

  • Log In with Google      Sign In   
  • Create Account


Multiple Iterators or just one?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 mapPhil   Members   -  Reputation: 103

Like
0Likes
Like

Posted 08 June 2011 - 05:09 AM

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.

Sponsor:

#2 SyncViews   Members   -  Reputation: 325

Like
1Likes
Like

Posted 08 June 2011 - 05:23 AM

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.

#3 szecs   Members   -  Reputation: 2102

Like
0Likes
Like

Posted 08 June 2011 - 05:24 AM

Well, you should decouple updates from rendering, so....

#4 rip-off   Moderators   -  Reputation: 8069

Like
1Likes
Like

Posted 08 June 2011 - 05:25 AM

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.

#5 Rattrap   Members   -  Reputation: 1565

Like
0Likes
Like

Posted 08 June 2011 - 06:53 AM

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).

#6 way2lazy2care   Members   -  Reputation: 782

Like
0Likes
Like

Posted 08 June 2011 - 08:37 AM

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.

#7 Storyyeller   Members   -  Reputation: 212

Like
0Likes
Like

Posted 08 June 2011 - 10:56 AM

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.
I trust exceptions about as far as I can throw them.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS