[Effective-cpp] Item 1: Uses and Abuses of vector
Adrian Fagg
adrianf at evaluategroup.com
Wed Oct 27 08:50:29 EDT 2004
> -----Original Message-----
> From: effective-cpp-bounces at accu.org
> [mailto:effective-cpp-bounces at accu.org]On Behalf Of Klitos Kyriacou
> Solution 1, const iterator outside the loop:
> const vector<int>::const_iterator end = vint.begin();
> for (vector<int>::const_iterator it = vint.begin(); it != end; ++it)
>
> Solution 2, non-const iterator inside the loop:
> for (vector<int>::const_iterator it = vint.begin(), end =
> vint.end(); it
> != end; ++it)
>
> I don't like solution 1 because the iterator 'end' has too large a
> scope. I only want it to be accessible up to the end of the
> loop, not to
> the end of the enclosing block.
Surely not the enclosing scope?
It does have too much scope though, being available in the loop body.
> Other languages (I think Fortran is
> one of them)
> solve this problem by having the language specification say that the
> compiler calculates the step and end values just once at the start of
> the loop and uses a saved copy of the values in each iteration.
Pascal does that as well.
The trouble is that C++ for loops are essentially while loops in different
clothes, which is another argument in favour of for_each etc. in my view.
Regards,
Adrian
Work: mailto:adrianf at evaluategroup.com
Home: mailto:adrian.fagg at ntlworld.com
Pretty pictures: http://homepage.ntlworld.com/rbyadf/
--- StripMime Report -- processed MIME parts ---
multipart/mixed
text/plain (text body -- kept)
application/ms-tnef
---
More information about the Effective-cpp
mailing list