April 2018
Beginner
714 pages
18h 21m
English
Qt provides the foreach macro for iterating over Qt containers:
QVector<int> x { 1, 2, 3 };
foreach(const int i, x) {
qDebug() << i;
}
This macro was available long before the range-based for loop made it into the C++ standard, so it's still very common in Qt code, and you should be familiar with it. The foreach loop always creates a temporary constant copy of the iterated object. Since it uses implicit sharing, this is very cheap. If you edit x while iterating over it, the changes will not affect the values of i because the iteration uses a copy, but this also means that such an operation is safe. Note that when using range-based for loop, STL-style iterators, or Java-style iterators, editing the same ...
Read now
Unlock full access