当使用
foreach
循环迭代QList<T>
时,根据我进行的测试,返回的项与标准for
循环中的项以相同的顺序返回。我的问题是,对于具有自然排序(如QList
和QVector
)的容器,foreach
是否总是按照索引数字顺序返回项目?例如,以下两种方式是否始终等效?QList<T> list;
for( int i=0; i<list.count(); ++i )
{
// process items in numerical order by index
// do something with "list[i]";
}
foreach( T item, list )
{
// will items always be processed in numerical order by index?
// do something with "item";
}
operator[]
运行良好。参见:http://qt-project.org/doc/qt-4.8/qlist.html#operator-5b-5d - Nikos C.