Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <iterator>
  6. #include <map>
  7. #include <vector>
  8. #include <algorithm>
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12. if (argc < 3)
  13. {
  14. std::cerr << "Not enough arguments" << std::endl;
  15. return 1;
  16. }
  17.  
  18. std::ifstream f(argv[1]);
  19. if (f.is_open())
  20. {
  21. std::map<std::string, int> m;
  22.  
  23. std::stringstream wss;
  24. wss << f.rdbuf();
  25. f.close();
  26.  
  27. std::string buf = "";
  28.  
  29. std::string line = wss.str();
  30. for (auto it : line)
  31. {
  32. if ((it > 64 && it < 91) || (it > 96 && it < 123))
  33. {
  34. buf += tolower(it);
  35. }
  36. else
  37. {
  38. if (buf.length() > 0)
  39. {
  40. m[buf]++;
  41. buf.clear();
  42. }
  43. }
  44. }
  45.  
  46. std::vector<std::pair<std::string, int> > v(m.size());
  47.  
  48. std::copy(m.begin(), m.end(), v.begin());
  49. m.clear();
  50.  
  51. std::sort
  52. (
  53. v.begin(), v.end(),
  54. [](std::pair<std::string, int> const & a, std::pair<std::string, int> const & b)
  55. {
  56. return a.second != b.second ? a.second > b.second : a.first < b.first;
  57. }
  58. );
  59.  
  60. std::ofstream of(argv[2]);
  61. if (of.is_open())
  62. {
  63. for (std::pair<std::string, int> p : v)
  64. {
  65. of << p.second << " " << p.first << std::endl;
  66. }
  67. of.close();
  68. }
  69. else
  70. {
  71. std::cerr << "Can't open file " << argv[2] << std::endl;
  72. return 1;
  73. }
  74. }
  75. else
  76. {
  77. std::cerr << "Can't open file " << argv[1] << std::endl;
  78. return 1;
  79. }
  80.  
  81. return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement