Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #include <vector>
  2. #include <map>
  3. #include <iostream>
  4. #include <string>
  5. #include <set>
  6. #include <algorithm>
  7. #include <iomanip>
  8. #include <fstream>
  9. #include <iterator>
  10. #include <sstream>
  11.  
  12. using namespace std;
  13.  
  14. string filter_punctuation(const string &helper){
  15. const char* forbidden{ ".,:; " };
  16. const auto idx_start(helper.find_first_not_of(forbidden));
  17. const auto idx_end(helper.find_first_not_of(forbidden));
  18. return helper.substr(idx_start, idx_end - idx_start + 1);
  19. }
  20.  
  21. ifstream in("input1.txt");
  22. ofstream out("output1.txt");
  23.  
  24. int main()
  25. {
  26. setlocale(LC_ALL, "RUS");
  27. int k,i;
  28. string vhod,str,strr;
  29. string helper = "";
  30. set<string> slova;
  31. map<string, int> words;
  32. ifstream in("input1.txt"); // окрываем файл для чтения
  33. if (in.is_open())
  34. {
  35. cout << "Входные данные: " << endl;
  36. while (getline(in, vhod))
  37. {
  38. cout << vhod << endl;
  39. }
  40. }
  41. in.close(); // закрываем файл
  42.  
  43.  
  44.  
  45.  
  46. for (size_t i = 0; i < vhod.length(); i++)
  47. {
  48. helper += vhod[i];
  49. if (vhod[i]==' ')
  50. {
  51. str += helper;
  52. helper = "";
  53. }
  54. }
  55. stringstream s1;
  56. s1.str(str);
  57. while (s1 >> strr) {
  58. ++words[strr];
  59. }
  60. for (auto it = words.begin(); it != words.end(); it++) {
  61. cout << (*it).first << ":" << (*it).second << endl;
  62. }
  63. vector<pair<string, int>>word_counts;
  64. word_counts.reserve(words.size());
  65. move(begin(words), end(words), back_inserter(word_counts));
  66. sort(begin(word_counts), end(word_counts), [](const auto& a, const auto& b) {return a.second > b.second; });
  67. for (auto it = word_counts.begin(); it != word_counts.end(); it++) {
  68. cout << (*it).first << ":" << (*it).second << endl;
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement