Sign in to follow this  
Followers 0
mapPhil

Multiple Iterators or just one?

6 posts in this topic

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

Share this post


Link to post
Share on other sites
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.
1

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:
[code]
i = list.begin();
while(i != list.end())
{
// use *i
++i;
}

// Later...

while(i != list.end())
{
// use *i
++i;
}
[/code]
The second loop won't execute because we forgot to reset the iterator.
1

Share this post


Link to post
Share on other sites
I've been watching [url="http://herbsutter.com/2011/05/20/my-lambdas-talk-nwcpp-is-now-online/"]Herb Sutter's lambda talks[/url] 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 [b]for each[/b] 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).
0

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

Share this post


Link to post
Share on other sites
[quote name='Rattrap' timestamp='1307537612' post='4820915']
I've been watching [url="http://herbsutter.com/2011/05/20/my-lambdas-talk-nwcpp-is-now-online/"]Herb Sutter's lambda talks[/url] 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 [b]for each[/b] 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).
[/quote]

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

Share this post


Link to post
Share on other sites

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  
Followers 0