c++ 容器遍历
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
map<string, int> m;
m["a"] = 0;
m["b"] = 1;
for(auto& it : m) {
it.second++;
}
for(auto it : m) {
cout<<it.first<<" "<<it.second<<endl;
}
return 0;
}
我之前是用iterator,用上面这种写法会比较简单,但是要记住这种遍历是值传递,如果需要修改需要加&