本文介绍了如何循环遍历地图的 C++ 地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!
问题描述
如何在 C++ 中循环遍历 std::map
?我的地图定义为:
How can I loop through a std::map
in C++? My map is defined as:
例如上面的容器保存的数据是这样的:
For example, the above container holds data like this:
如何遍历此地图并访问各种值?
How can I loop through this map and access the various values?
推荐答案
老问题,但从 C++11 开始,其余答案已过时 - 您可以使用 范围基于 for 循环 只需执行:
Old question but the remaining answers are outdated as of C++11 - you can use a ranged based for loop and simply do:
这应该比早期版本更干净,并避免不必要的副本.
this should be much cleaner than the earlier versions, and avoids unnecessary copies.
有些人赞成用引用变量的显式定义替换注释(如果未使用,则会被优化掉):
Some favour replacing the comments with explicit definitions of reference variables (which get optimised away if unused):
这篇关于如何循环遍历地图的 C++ 地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!