Public Group

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.

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 on other sites
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 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 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 on other sites
seed += (*oIt)->mTriangles.begin() + 1;

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

Needs to be:

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

Share on other sites
Quote:
 Original post by Antonyml_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 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).

1. 1
2. 2
3. 3
Rutin
21
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 10
• Forum Statistics

• Total Topics
633653
• Total Posts
3013149
×