Qt: 访问容器(三种方法,加上for循环就四种了)good

时间:2021-12-07 03:01:39

#include <iostream>
#include <QString>
#include <QList>
#include <QListIterator>

int main(int argc, char *argv[]) {
QList<QString> list;
list << "One" << "Two" << "Three";

foreach(QString s, list) {
std::cout << s.toStdString() << std::endl;
}

QListIterator<QString> iter(list);
while (iter.hasNext()) {
std::cout << iter.next().toStdString() << std::endl;
}

QList<QString>::Iterator stlIter; // QList<QString>::iterator stlIter; // Ok
for (stlIter = list.begin(); stlIter != list.end(); ++stlIter) {
std::cout << stlIter->toStdString() << std::endl;
}

return 0;
}

http://www.cppblog.com/biao/archive/2008/03/21/45049.html