fork download
  1. /** Count words.
  2.  
  3.   One word per line.
  4.   Print words in lexicographical order.
  5.  
  6.   To try:
  7.  
  8.   $ g++ -std=c++11 count-words-map.cpp && ./a.out <input
  9.   One 3
  10.   Three 1
  11.   Two 1
  12.  
  13. */
  14. #include <iostream>
  15. #include <map>
  16.  
  17. int main() {
  18. using namespace std;
  19. map<string, size_t> counter;
  20. string line;
  21. getline(cin, line); // read n and ignore it
  22. while(getline(cin, line)) // count words
  23. ++counter[line];
  24.  
  25. if (!cin.eof())
  26. return 1; // I/O error
  27.  
  28. // print words and their frequencies
  29. for (const auto& p : counter)
  30. cout << p.first << " " << p.second << endl;
  31. }
  32.  
Success #stdin #stdout 0s 3436KB
stdin
5
One
Two
Three
One
One
stdout
One 3
Three 1
Two 1