Use of foreach in QT

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.