Jump to content
  • Advertisement
Sign in to follow this  
Sean_Seanston

How to make Iterators start/stop at a certain point?

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

I can't figure out a fairly simple thing with iterators here...

Let's say you want an std::list iterator to stop 1 position before the end. How would you do that?

I tried:


std::list< int >::iterator it;
for( it = list.begin(); it != list.end() - 1; it++ )
{
}


But that doesn't work.
I can get around it by defining another iterator like:

std::list< int >::iterator it2 = list.end();
it2--;


Then putting that iterator in the loop but that can't be the only way surely.

advance() doesn't seem the thing to use so... I'm out of ideas after Google came up with nothing but it's a hard question to word correctly.

Share this post


Link to post
Share on other sites
Advertisement



Let's say you want an std::list iterator to stop 1 position before the end. How would you do that?

I tried:


std::list< int >::iterator it;
for( it = list.begin(); it != list.end() - 1; it++ )
{
}





Try

std::list<int>::iterator it;
for( it=list.begin(); it!=--list.end(); ++it ){
}


Share this post


Link to post
Share on other sites

Seems like you want to reference the last element, you can do so using std::list.back() method.


Well, not necessarily... I also need to reference an element 1 after another iterator so I need to be able to reference a position in relation to another iterator.


Try

std::list<int>::iterator it;
for( it=list.begin(); it!=--list.end(); ++it ){
}


If it was a iterator you needed to keep track of though, instead of list.end(), that'd increment it and I'd still need another iterator...

One of the things I want to do is reference the position after another iterator, but I can't modify the other iterator because I need to keep track of that too.

Share this post


Link to post
Share on other sites
The general alternative to "[font="'Courier New"]iterator + offset[/font]" is "[font="'Courier New"]std::advance(iterator, offset)[/font]"

Share this post


Link to post
Share on other sites

The general alternative to "[font="Courier New"]iterator + offset[/font]" is "[font="Courier New"]std::advance(iterator, offset)[/font]"


But does that alter the iterator passed as a parameter, or just return an iterator equivalent to the iterator + the offset?

Checked cplusplus.com, it alters the iterator alright.

So, is there no way of getting an expression equivalent to an iterator + offset without altering the iterator or just creating a new named iterator based on the 1st and then offsetting that?

Share this post


Link to post
Share on other sites
This has a nice table on the capabilities of different iterator types; list's iterators are bidirectional, but not random access, so you can't just go iterator + constant, because it's not part of the operations it supports... But if you used a deque or vector to store your values, though, you could.

Share this post


Link to post
Share on other sites
You can use std::prev and std::next that is part of c++0x

std::list<int>::iterator it;
for( it=list.begin(); it!= std::prev(list.end()); ++it ){
}

Share this post


Link to post
Share on other sites

This has a nice table on the capabilities of different iterator types; list's iterators are bidirectional, but not random access, so you can't just go iterator + constant, because it's not part of the operations it supports... But if you used a deque or vector to store your values, though, you could.


I see... that explains that then. Thanks.

Might just leave it how it is then or see if there's a point changing it to a vector...

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!