|
foreach() ?foreach(變量,容器) ?在qt中foreach是一個關(guān)鍵字 ,類似于c語言的for循環(huán) ?原文: ?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關(guān)鍵字。關(guān)鍵字是對C++語言的一個特定于qt的添加,并且是使用預(yù)處理程序?qū)崿F(xiàn)的。
它的語法是: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: ?翻譯:除非數(shù)據(jù)類型包含一個逗號(例如,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++循環(huán)結(jié)構(gòu)一樣,您可以在foreach循環(huán)的主體周圍使用牙套,您可以使用break來離開循環(huán): ?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;
}
// 輸出結(jié)果為:
abc
qwe
upo
?
|