在我正在处理的一些代码中,我有一个遍历地图的 for 循环:
for (auto it = map.begin(); it != map.end(); ++it) {
//do stuff here
}
我想知道是否有某种方法可以简洁地写出以下内容:
for (auto it = map.begin(); it != map.end(); ++it) {
//do stuff here
} else {
//Do something here since it was already equal to map.end()
}
我知道我可以重写为:
auto it = map.begin();
if (it != map.end(){
while ( it != map.end() ){
//do stuff here
++it;
}
} else {
//stuff
}
但是有没有更好的方法不涉及包装在 if 语句中?