直接上代码:
QStringList test;
test.append("aaaa");
test.append("bbbb");
test.append("cccc");
QString tempa;
QString tempb;
int i = 0;
foreach(tempa, test)
{
qDebug() << tempa << endl;
if(1==i)
tempb = tempa;
i++;
}
qDebug() << tempb << endl;
运行结果:
上段代码,清晰的揭示了,foreach函数的作用,即将test里面的元素,通过循环传递给tempa。
所以博主猜测,该函数主要作用于list 类对象中。