For that reason, iterators are rarely useful in connection with QList. One place where STL-style iterators do make sense is as arguments to generic
http://doc.qt.digia.com/4.2/qlist-iterator.html
So if I have a for loop, where the macro foreach isn't an option. Should I use iterators or indexes?
for(int i = 0; i < list.size(); i++) {
Do something with list[i]
blah blah blah
}
for(QList<type>::iterator i = list.begin(); i != list.end(); ++i) {
Do something with *i
blah blah blah
}
Qt has it own foreach macro. I also can´t think of a reason why foreach is not an option...
The most important difference is whether you need to modify the list contents in the loop, as this may get harder to read with index API.
If you just want to do something for each of the items i have those preferences:
But it really is just a question of style. If this is something so critical for performance that you need to worry about the minor differences you should think about redesign or writing your own specialized list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With