Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <functional>
- #include <iostream>
- #include <sstream>
- #include <execution>
- #include <future>
- #include <numeric>
- #include <vector>
- #include <map>
- #include <set>
- #include <string>
- using namespace std;
- const int SIZE_OF_PART = 5000;
- struct Stats {
- map<string, int> word_frequences;
- void operator+=(const Stats& other) {
- for (const auto& [word, frequency] : other.word_frequences) {
- word_frequences[word] += frequency;
- }
- // for_each(execution::par,
- // other.word_frequences.begin(), other.word_frequences.end(),
- // [this](auto& item) { this->word_frequences[item.first] += item.second; });
- }
- };
- using KeyWords = set<string, less<>>;
- Stats ExploreKeyWords(const KeyWords& key_words, istream& input) {
- vector<vector<string>> parts_of_stream;
- // разделю поток на части по SIZE_OF_PART слов
- string word;
- int count = SIZE_OF_PART;
- while (!input.eof()) {
- vector<string> tmp;
- while (count) {
- input >> word;
- tmp.push_back(move(word));
- --count;
- }
- parts_of_stream.push_back(move(tmp));
- count = SIZE_OF_PART;
- }
- auto counter = [&key_words](const vector<string>& words) {
- Stats st;
- // for_each(execution::par,
- // words.begin(), words.end(),
- // [&st, &key_words](const string& word) {
- // if (key_words.count(word)) {
- // ++st.word_frequences[word];
- // }
- // }
- // );
- for (const string& w : words) {
- if (key_words.count(w)) {
- ++st.word_frequences[w];
- }
- }
- return st;
- };
- vector<future<Stats>> threads;
- for (const vector<string>& part : parts_of_stream) {
- // асинхронный запуск подсчетов для каждой из частей потока, разделенного на векторы слов
- threads.push_back(async(counter, cref(part)));
- }
- Stats result;
- for (future<Stats>& pre : threads) {
- result += pre.get();
- }
- return result;
- }
- int main() {
- const KeyWords key_words = {"yangle", "rocks", "sucks", "all"};
- stringstream ss;
- ss << "this new yangle service really rocks\n";
- ss << "It sucks when yangle isn't available\n";
- ss << "10 reasons why yangle is the best IT company\n";
- ss << "yangle rocks others suck\n";
- ss << "Goondex really sucks, but yangle rocks. Use yangle\n";
- for (const auto& [word, frequency] : ExploreKeyWords(key_words, ss).word_frequences) {
- cout << word << " " << frequency << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement