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;
}
// 输出结果为:
abc
qwe
upo
|