11.20 使用insert代替下标操作。
#include<iostream> #include<map> #include<string> #include<utility> using namespace std; int main() { map<string,size_t> word_count; string word; while(cin>>word) { auto ret=word_count.insert({word,1}); if(!ret.second) ++ret.first->second; } auto map_it=word_count.begin(); while(map_it!=word_count.end()) { cout<<map_it->first<<" "<<map_it->second<<endl; ++map_it; } return 0; }