Kai-Uwe Bux wrote:
Leigh Johnston wrote:
On 13/04/2011 22:48, Kai-Uwe Bux wrote:
Nikos Chantziaras wrote:
Is there some simple way to convert this:
std::vector<int> vec;
// ...
for (size_t i = 1; i< vec.size(); ++i)
// do something with vec[i]
to a for-loop that uses iterators? I've tried:
typedef std::vector<int>::iterator it_t;
for (it_t i = vec.begin() + 1; i != vec.end(); ++i)
// do something with *i
but I'm not surprised that it doesn't work.
Iterators for std::vector<> are random access. Hence, they
are<-comparable and you can add integers to them. So, you should be able
to translate one- to-one:
for ( std::vector<int>::iterator iter = vec.begin() + 1;
iter< vec.end(); ++ iter ) {
// do something with *iter
}
If the vector is empty then surely vec.begin() + 1 is UB?
Hm, my first impulse was to say: "oh, true; that kills it". However, upon
looking up table 76 in the standard, it appears that
r += n
has no pre-condition (like r is dereferencable). Now, r+n is defined in
terms of r += n. Also, the dereferencability precondition is mentioned at
other places (e.g., for ++r in table 74 about forward iterators). So,
maybe vec.begin()+1 is not undefined behavior after all. Of course, it
could also be an omission in the standard, or it could be that I am too
tired to figure it out correctly.
Seems, it was the third option:
[24.1/5] Iterators can also have singular values that are not associated
with any container. [Example: After the declaration of an uninitialized
pointer x (as with int* x;), x must always be assumed to have a singular
value of a pointer. ] Results of most expressions are undefined for
singular values; the only exception is an assignment of a non-singular
value to an iterator that holds a singular value. In this case the
singular value is overwritten the same way as any other value.
Dereferenceable values are always non-singular.
Hence, vec.begin()+1 is not UB (as vec.begin() is non-singular), but
assigning the resulting singular value to the iterator variable iter is UB.
pointers could not be iterators since (arr + sizeof(arr))++ is UB.
Luckily I believe you're interpreting incorrectly. The quote you show
there quite explicitly calls "most expressions are undefined...". The
only exception it provides is assignment FROM a non-singular. It seems
iterates something. Further it isn't allowing for incrementing an
iterator past end().