C++ / Qt 循环关键字 的明晰

    //简要介绍:
    /*
     * 使用C++和Qt大家都知道 循环的关键字C++有 for ,智能for, Qt 有foreach。
     * 用来循环QMap这种存放键值对的类型的时候,对于智能for和Qt的foreach的循环变量针对的是什么,小熊博士总是含糊,使用起来不清晰。
    */

    QMap<int, QString> map{{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}};
    qDebug()<<"map is: "<<"QMap<int, QString> map{{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}}-----

";

    //C++智能for
    qDebug()<<"C++智能for:";
    for (auto var : map){
        qDebug()<<var<<" ";
    }

    qDebug()<<"-----
";
    //qt 的 foreach
    qDebug()<<"qt 的 foreach:";
    foreach (auto var, map){
        qDebug()<<var<<" ";
    }
//执行结果

11:04:53: Starting E:Qt_projcetplayuild-play-Desktop_Qt_5_12_2_MinGW_64_bit-Debugdebugplay.exe...
map is:  QMap<int, QString> map{{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}}-----


C++智能for:
"one"  
"two"  
"three"  
"four"  
-----

qt 的 foreach:
"one"  
"two"  
"three"  
"four"  
原文地址:https://www.cnblogs.com/azbane/p/12530393.html