i wrote a small test app the other day to answer the performance quandry. turns out that for collections of POD types, stl and foreach are essentially equivalent (no surprise, since foreach is syntactic sugar for an stl style iteration) while java style is slightly slower. for more complex types (i tested with QList<QString>) foreach is within 10% and java style is within 20% of stl style.
but you have to be careful. for instance:
List::const_iterator itEnd = list.constEnd();
for (List::const_iterator it = list.constBegin();
it != itEnd;
// do something
if you don't (e.g. because you can't) use const_iterators, it's measurably slower. if you don't cache the constEnd you also take a large time penalty. and if you invalidate the iterator while iterating (e.g. you remove an element) you need extra code to ensure your loop doesn't end up resulting in poor results (e.g. crashing)
foreach (const ListType& i, list)
// do something
note the use of "const ListType& i"; with just "ListType i" where ListType is a QString you'll incur a good 40% performance penalty due to the code that foreach expands out to. (thanks to matthias ettrich for pointing this one out to me on irc)
so what are the actual differences in times? iterating over a 1,000,000 element collection of QStrings using each of the three methods results in data that tends to look like this:
list has 1000000 items in it
start for loop: "28 118"
start java iterator: "28 481"
start foreach: "28 904"
the numbers came from running the test app on a 1.7GHz laptop and are in milliseconds, so you can see that we're not talking significant (e.g. user-visible) differences even with a set of a million strings.