in standard C + +, there is no foreach keyword.
But in Qt, you can use this keyword, the main reason is that QT itself added this keyword, like slots and signals, emit and so on. The added foreach keyword is preprocessed at compile time.
The usage is:
foreach (Varitem, Items) // foreach (variable, container)
where Varitem (variable) is an item in the container items (container), which is equivalent to: Variable=container.item. The traversal is traversed from the beginning to the tail.
such as the following code:
Qstringlist SLT = {"ABC""qwe" "Upo" }; foreach (QString s, slt) { cout<<s<<Endl;} // the output is: Abcqweupo
In languages such as C #, Java, you can use foreach directly, with keywords for the language and similar usage.
Among them, in the C++11 standard, a new statement is provided: a range for statement. Similar to the Foreach usage here. Only the ":" Instead of "," is used here.
Its grammatical form is:
for (declaration:expression)
The following code is used: (Refer to the foreach code)
Qstringlist SLT = {"ABC""qwe" "Upo" }; for (QString s:slt) { cout<<s<<Endl;} // the output is: Abcqweupo
Use of foreach in QT