Jump to content
  • Advertisement
Sign in to follow this  
Antonym

incrementing list iterator

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

Is there an equivalent of doing
std::list<val>::iterator l_it;

l_it += 2;
l_it -= 4;



etc Without using ++ and --. I know that with vectors the above operations are valid but is there a way of doing the same with lists? Also, a second question, if I delete an element in a list that an iterator is pointing to, what happens to the iterator? Does it become invalid or does it now point to one of the adjacent elements? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
http://www.cplusplus.com/reference/std/iterator/

It says += will work. I have never done it before, but did it not compile?

When you delete an object the data is garbage. You can do:

list<type>::iterator iter = YourList.begin();

iter = YourList.erase(iter);

*The erase function returns the next object in the list.

Share this post


Link to post
Share on other sites
No it doesn't compile :S.



seed += (*oIt)->mTriangles.begin() + 1;




1>c:\documents and settings\david\mis documentos\visual studio 2008\projects\marauders\dungeon\playstate.cpp(76) : error C2678: binary '+' : no operator found which takes a left-hand operand of type 'std::list<_Ty>::_Iterator<_Secure_validation>' (or there is no acceptable conversion)
1> with
1> [
1> _Ty=b2Vec2,
1> _Secure_validation=true
1> ]
1> c:\box2d\source\common\b2math.h(554): could be 'b2Vec2 operator +(const b2Vec2 &,const b2Vec2 &)'
1> c:\box2d\source\common\b2math.h(593): or 'b2Vec3 operator +(const b2Vec3 &,const b2Vec3 &)'
1> c:\box2d\source\common\b2math.h(616): or 'b2Mat22 operator +(const b2Mat22 &,const b2Mat22 &)'
1> while trying to match the argument list '(std::list<_Ty>::_Iterator<_Secure_validation>, int)'
1> with
1> [
1> _Ty=b2Vec2,
1> _Secure_validation=true
1> ]

Thanks for the iter = YourList.erase(iter);

Share this post


Link to post
Share on other sites
IIRC you can write:
std::advance(l_it, 2);
std::advance(l_it, -4);

For random access iterators, this will compile down to "+=" or "-=", but for list iterators it will compile to "while(n--) it++" or "while(n--) it--"


std::distance uses the same technique to implement "it1 - it2" for both random-access and list iterators.

Share this post


Link to post
Share on other sites
seed += (*oIt)->mTriangles.begin() + 1;


It is complaining about the '+' not '+='.

Needs to be:


seed = (*oIt)->mTriangles.begin();
seed += 1;

Share this post


Link to post
Share on other sites
Quote:
Original post by Antonym
l_it += 2;
l_it -= 4;


Hopefully you're aware that this approach is very inefficient on long lists. To go back, say, 100 elements with a list is 100 operations (following 100 back pointers), whereas with a vector it would be just 1 operation (decreasing an index by 100). If this concerns you and you aren't sure what to do about it, please say so.

Share this post


Link to post
Share on other sites
No, I wasn't aware of that, thanks. I'll be sticking to vectors, the reason I switched to lists to begin with was because I didn't want to lose my iterators. Didn't know you could do iter = erase(iter).

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!