fork(1) download
  1. #include <map>
  2. #include <iostream>
  3. #include <iterator>
  4. #include <algorithm>
  5. #include <string>
  6. #include <fstream>
  7. #include <iomanip>
  8. #include <locale>
  9. #include <vector>
  10. #include <unordered_set>
  11.  
  12. struct alpha_only: std::ctype<char> {
  13. alpha_only() : std::ctype<char>(get_table()) {}
  14.  
  15. static std::ctype_base::mask const* get_table() {
  16. static std::vector<std::ctype_base::mask>
  17. rc(std::ctype<char>::table_size,std::ctype_base::space);
  18. for (int i=0; i<std::ctype<char>::table_size; i++)
  19. if (isalpha(i)) rc[i] = std::ctype_base::alpha;
  20. rc['\''] = std::ctype_base::alpha;
  21. return &rc[0];
  22. }
  23. };
  24.  
  25. typedef std::pair<std::string, unsigned> count;
  26.  
  27. namespace std {
  28. std::ostream &operator<<(std::ostream &os, ::count const &c) {
  29. return os << std::left << std::setw(25) << c.first
  30. << std::setw(10) << c.second;
  31. }
  32. }
  33.  
  34. int main(int argc, char **argv) {
  35. std::locale loc(std::locale(), new alpha_only());
  36. for (int i=1; i<argc; ++i) {
  37. std::ifstream input(argv[i]);
  38. input.imbue(loc);
  39.  
  40. std::map<std::string, unsigned> words;
  41.  
  42. std::for_each(std::istream_iterator<std::string>(input),
  43. std::istream_iterator<std::string>(),
  44. [&words](std::string const &w) { ++words[w]; });
  45. std::copy(words.begin(), words.end(),
  46. std::ostream_iterator<count>(std::cout, "\n"));
  47. }
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0s 3024KB
stdin
Standard input is empty
stdout
Standard output is empty