### Iterators past limits question

Hi,
I'm having trouble finding documentation about iterator operations when they go too far. In particular, say I want to advance an iterator by 50 or -50 typically, but, obviously at the end of a container or beginning of a container this will sometimes not land on begin() or end() and potentially go beyond where there is actually an iterator. If I have a set<double>mySet, for example, let me ask some specific circumstantial questions:
(1) If `mySet.size()==0`, then does mySet.begin()==mySet.end()? or is it somehow undefined?
(2) If `set<double>::iterator it=mySet.begin();it--;` What happens? Does it==mySet.end(), mySet.begin() still, or something buggy?
(3) If `set<double>::iterator it=mySet.end();it++;` What happens? Does it==mySet.end(), mySet.begin() still, or something buggy?
(4) If `set<double>::iterator it=mySet.begin();advance(it,-10);` What happens? Does it==mySet.end(), mySet.begin() still, or something buggy?
(5) If `set<double>::iterator it=mySet.end();advance(it,+10);` What happens? Does it==mySet.end(), mySet.begin() still, or something buggy?
Last edited on
(1) Yes

(2..5) Something buggy. No STL algorithm will forbid you from endeavoring to advance an iterator outside of its proper bounds. You must enforce those bounds yourself.

This is obnoxious, but consistent with pointer arithmetic.

Hope this helps.

 BTW, don't ever try to advance an iterator outside its bounds and assume that you are still in the land of defined-ness.
Last edited on
Topic archived. No new replies allowed.