//简要介绍:
/*
* 使用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\"}}-----\n\n";
//C++智能for
qDebug()<<"C++智能for:";
for (auto var : map){
qDebug()<<var<<" ";
}
qDebug()<<"-----\n";
//qt 的 foreach
qDebug()<<"qt 的 foreach:";
foreach (auto var, map){
qDebug()<<var<<" ";
}
//执行结果
11:04:53: Starting E:\Qt_projcet\play\build-play-Desktop_Qt_5_12_2_MinGW_64_bit-Debug\debug\play.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"