不使用迭代器就可以在c ++中打印地图吗?就像是
map <int, int>m;
m[0]=1;
m[1]=2;
for(int i =0; i<m.size(); i++)
std::cout << m[i];
是否有必要使迭代器打印地图值?
如果只想避免键入迭代器样板,则可以使用range-for循环来打印每个项目:
#include <iostream>
#include <map>
int main() {
std::map<int,std::string> m = {{1, "one"}, {2, "two"}, {3, "three"}};
for (const auto& x : m) {
std::cout << x.first << ": " << x.second << "\n";
}
return 0;
}
实时示例:http://coliru.stacked-crooked.com/a/b5f7eac88d67dafe
适用范围:http : //en.cppreference.com/w/cpp/language/range-for
显然,这在后台使用了地图的迭代器...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句