Qt has its own foreach macro. I also can't think of why foreach is not an option ...
The most important difference is whether you need to change the contents of the list in a loop, as this can be more difficult to read using the index APIs.
If you just want to do something for each of the elements, I have these settings:
- Use foreach with a (const-) reference type and let the framework do the right thing
- Use api :: at (int) index (Quote from 4.7 docs: "QList is implemented in such a way that index-based direct access is as fast as iterators" and "at () can be faster than the [] operator"
- Use Iterator API
But this is really a matter of style. If this is so important for performance that you need to worry about minor differences, you should consider redesigning or writing your own custom list.
source share