If you just want to iterate over all the items in a container in order, you can use Qt'sforeach 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 useforeach to iterate over aQLinkedList<QString>:
QLinkedList<QString> list; ... QString str; foreach (str, list) qDebug() << str;如果你想有序迭代容器中的所有项可以使用关键字foreach,这是qt对C++的特定补充,并通过预处理器实现。他的语法是:foreach (variable,container) +语句;这儿varible就相当于varible=container.item,只不过这个item会从container的头遍历到尾罢了。
QLinkedList<QString> list;QLinkedListIterator<QString> i(list); while (i.hasNext()) qDebug() << i.next();这两个语句达到的目的是一样的
本文介绍了如何在Qt中使用foreach关键字来迭代容器中的元素。foreach是Qt为C++提供的扩展,通过预处理器实现,简化了对容器内元素的遍历操作。文中还对比了使用迭代器的方式。
423

被折叠的 条评论
为什么被折叠?



