word counting using C++ STL map and set


👍 cat stevejobs.txt 
innovation distinguishes between 
a leader and a follower

👍 g++ -std=c++11 wordcounting.cpp 
👍 ./a.out < stevejobs.txt 
between occurs 1 time
distinguishes occurs 1 time
follower occurs 1 time
innovation occurs 1 time
leader occurs 1 time

👍 cat wordcount.cpp 
#include<iostream>
#include<map>
#include<set>
using namespace std;
int main(){
  map<string, size_t> word_count;
  set<string> exclude = {
    "The", "the", "And", "and", 
    "An", "an", "A", "a"};
  string word;
  while(cin >> word)
    if (exclude.find(word) ==
        exclude.end())
      ++word_count[word];
  for(const auto &w: word_count)
    cout << w.first << " occurs " 
    << w.second
    << (w.second>1?" times":" time")
    << endl;
}