Re: Why std::size_t instead of int? (was Re: Do I really have to use
an array?)
On Feb 6, 6:04 pm, "Alf P. Steinbach" <al...@start.no> wrote:
* Andrew Koenig:
"Alf P. Steinbach" <al...@start.no> wrote in message
news:13qjhorqbs7rkc3@corp.supernews.com...
If you want the range myLenght-1 downto 0, inclusive,
consider just
for( size_t i = myLength-1; i != size_t(-1); --i )
I have to say that this example is too clever for my taste.
Because it depends on the modulo arithmetic of unsigned types?
(I'll admit that I'm not too fond of it for this reason either.
It's not a context where I'd expect modulo arithmetic.)
I'd rather write:
for (size_t i = myLength; i != 0; ) {
--i;
// whatever
}
I have to say that that example is too clever for /my/ taste... ;-)
With a for-loop, except "for(;;)", one expects the expression
that in some sense "counts" (or less informally speaking,
controls the loop variant), to be in the loop head, after that
last semicolon.
Also, one expects the initializer to be the first value for
the loop control variable.
So write it:
size_t i = myLength ;
while ( i != 0 ) {
-- i ;
// ...
}
There seems to be a great deal of variance with regards to what
is considered acceptable in a for. Like you, I more or less
expect to find three parts (although it doesn't bother me too
much if the first is missing), and more importantly, that if the
control variable is modified in some way, that modification
takes place in the third part, and not elsewhere. Which means
that in this case, I would use a while, and not a for.
One reason for my preference is that this technique works
for bidirectional iterators too:
for (list<T>::iterator it = myList.end(); it != myList.begin(); =
) {
--it;
// whatever
}
whereas the "myLength-1" technique doesn't.
Hm. I don't think it's necessarily a good idea to press
different kinds of code into the same form, or using the
possibility of doing so as a positive guideline. And I think
the above would be better expressed using reverse iterators,
placing the update in the for loop head, like
typedef list<T>::reverse_iterator RIterator;
for( RIterator it = myList.rbegin(); it != myList.rend(); ++it )
{
// Whatever
}
This is definitly the way the STL was designed to be used.
Whether it is a good idea or not is another question. It sort
of hides the fact that you are going backwards, which may be the
sort of necessary information you don't want to hide. I also
find it rather confusing that when you extract the base
iterator, it is off by one.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34