Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <map>
- #include <iostream>
- #include <string>
- #include <set>
- #include <algorithm>
- #include <iomanip>
- #include <fstream>
- #include <iterator>
- #include <sstream>
- using namespace std;
- string filter_punctuation(const string &helper){
- const char* forbidden{ ".,:; " };
- const auto idx_start(helper.find_first_not_of(forbidden));
- const auto idx_end(helper.find_first_not_of(forbidden));
- return helper.substr(idx_start, idx_end - idx_start + 1);
- }
- ifstream in("input1.txt");
- ofstream out("output1.txt");
- int main()
- {
- setlocale(LC_ALL, "RUS");
- int k,i;
- string vhod,str,strr;
- string helper = "";
- set<string> slova;
- map<string, int> words;
- ifstream in("input1.txt"); // окрываем файл для чтения
- if (in.is_open())
- {
- cout << "Входные данные: " << endl;
- while (getline(in, vhod))
- {
- cout << vhod << endl;
- }
- }
- in.close(); // закрываем файл
- for (size_t i = 0; i < vhod.length(); i++)
- {
- helper += vhod[i];
- if (vhod[i]==' ')
- {
- str += helper;
- helper = "";
- }
- }
- stringstream s1;
- s1.str(str);
- while (s1 >> strr) {
- ++words[strr];
- }
- for (auto it = words.begin(); it != words.end(); it++) {
- cout << (*it).first << ":" << (*it).second << endl;
- }
- vector<pair<string, int>>word_counts;
- word_counts.reserve(words.size());
- move(begin(words), end(words), back_inserter(word_counts));
- sort(begin(word_counts), end(word_counts), [](const auto& a, const auto& b) {return a.second > b.second; });
- for (auto it = word_counts.begin(); it != word_counts.end(); it++) {
- cout << (*it).first << ":" << (*it).second << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement