On Feb 9, 12:34 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
From your replies above, what I understand is that:
when we specify an iterator range, to a container operation or an
std::algorithm, both the first iterator and the last iterator can be
the sentinel ie both of them can be off-the-end iterators. correct?
For all of the algorithms in the standard library, at any rate.
If so, how do we know whether the implementation of container
member operations or the std:algorithms which accept an input
range, will not dereference the first iterator when both the
first and last are sentinel.
The same way that you know that it won't start by incrementing
the begin iterator three times, even if there are only two
elements in the sequence. The specifications of the function.
(Presumably, you could write a function which has a
pre-condition that distance( begin, end ) >= 3. There are
probably even some strange cases where it makes sense.)
The specifications of all of the functions in the standard
library, and most reasonably defined user functions, allows
empty sequences.
Essentially how do we know that the following operations'
definitions on all compiler implementations will not
dereference the the first iterator which is already the
sentinel because the container is empty.
vector<stringv;
v.erase(v.begin(), v.end());
sort(v.begin(), v.end());
Because the specifications of the functions in the standard say
so.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34