Re: Reasonable expectation: for(it = v.begin(); it != v.end(); it++)

From: Steven T. Hatton (susudata_at_setidava.kushan.aa)
Date: 08/16/04


Date: Mon, 16 Aug 2004 07:26:13 -0400

Richard Herring wrote:

>
> If you're really, justifiably (i.e. a profiler told you so!) worried
> about the cost of repeatedly calling v.end(), why not just do this:
>
> for (whatever::iterator it=v.begin(), e=e.end(); it!=e; ++it) {...}
>
> ?

I haven't started messing with a profiler yet - it's not a bad suggestion -,
but in some of the things I do, number crunching is an issue.

Yes, that is an option. For the sake of readability, I would prefer:

whatever::iterator e=v.end();
for (whatever::iterator it=v.begin(); it!=e; ++it) {...}

That will work 99.9% of the time. I am very unlikely to try to grow the
vector in the for() loop, however...

-- 
STH 
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org  SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org


Relevant Pages