C++ foreach()与for()的用法+Qt

foreach()

  • 依次读取数组的每个元素
void  testForeach(){
    QStringList listTemp;
    listTemp.append("aaa");
    listTemp.append("bbb");
    listTemp.append("ccc");
    QString strTemp1;
    QString strTemp2;

    foreach(strTemp1,listTemp)
    {
       strTemp2= strTemp1;
       qDebug()<<strTemp1;
    }

}
  • Qt中for()也有类似的用法
void  testFor(){
    QStringList listTemp;
    listTemp.append("aaa");
    listTemp.append("bbb");
    listTemp.append("ccc");

    for(QString strTemp1 : listTemp)
    {
        qDebug()<<strTemp1;
    }
}
  • 以上程序的运行结果一致,见下
    这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_25188995/article/details/81407089
今日推荐