打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
QT中foreach()的使用方法
userphoto

2018.07.14

关注
foreach()
​foreach(变量,容器)
​在qt中foreach是一个关键字 ,类似于c语言的for循环
​原文:
​The foreach Keyword
​If you just want to iterate over all the items in a container in order, you can use Qt's foreach keyword. The keyword is a Qt-specific addition to the C++ language, and is implemented using the preprocessor.
​Its syntax is: foreach (variable, container) statement. For example, here's how to use foreach to iterate over a QLinkedList<QString>:

​翻译:

如果您只是想遍历容器中的所有条目,那么您可以使用Qt的foreach关键字。关键字是对C++语言的一个特定于qt的添加,并且是使用预处理程序实现的。

它的语法是:foreach(变量,容器)语句。例如,下面是如何使用foreach来迭代QLinkedList:
​QLinkedList<QString> list;
...
QString str;
foreach (str, list)
    qDebug() << str;


​原文:The foreach code is significantly shorter than the equivalent code that uses iterators:
​翻译:foreach代码比使用迭代器的等效代码要短得多:
QLinkedList<QString> list;
...
QLinkedListIterator<QString> i(list);
while (i.hasNext())
    qDebug() << i.next();

​原文:Unless the data type contains a comma (e.g., QPair<int, int>), the variable used for iteration can be defined within the foreach statement:
​翻译:除非数据类型包含一个逗号(例如,QPair<int,int),否则用于迭代的变量可以在foreach语句中定义:
QLinkedList<QString> list;
...
foreach (const QString &str, list)
    qDebug() << str;

原文:And like any other C++ loop construct, you can use braces around the body of a foreach loop, and you can use break to leave the loop:
​翻译:就像任何其他C++循环结构一样,您可以在foreach循环的主体周围使用牙套,您可以使用break来离开循环:
QLinkedList<QString> list;
...
foreach (const QString &str, list) {
    if (str.isEmpty())
        break;
    qDebug() << str;
}
例子:

QStringList slt = {"abc", "qwe", "upo"};foreach(QString s , slt ){    cout<<s<<endl;}// 输出结果为:abcqweupo


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
第2章 Qt 5模板库、工具类及控件
Qt 字符串设置固定宽度,前位补0
QT学习笔记-7.QString和QByteArray - Cpper - C++博客
QT 正则表达式 通常用
Qt那点事儿(二)
qt 学习笔记(转载)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服