++counters[s]; is the same as: ++(counters.operator[](s)) --------------------- std::map cats; cats[100] = 10.0; cats[100000] = sqrt(100000); cats[5] = sqrt(5); cats[505] = sqrt(505); for (std::map::const_iterator itr = cats.begin(); itr != cats.end();itr++) { std::cout << (*itr).first << " " << itr->second << std::endl; } --------------------- int main() { std::string s; // store each word and an associated count std::map counters; // read the input, keeping track of each word and how often we see it // n is # of rows currently in table/map while (std::cin >> s) { //++counters[s]; std::map::iterator itr = counters.find(s); // O(log n) if (itr == counters.end()) { // not yet seen this word std::pair my_pair(s,1); counters.insert(my_pair); // O(log n) // also the same thing: //counters.insert(std::pair(s,1)); //counters.insert(std::make_pair(s,1)); } else { // increment the pair assert (itr->first == s); ++itr->second; // O(1) //++counters[s]; } } // write the words and associated counts std::map::const_iterator it; for (it = counters.begin(); it != counters.end(); ++it) { std::cout << it->first << "\t" << it->second << std::endl; } return 0; }