Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <functional>
- #include <iostream>
- #include <map>
- #include <set>
- #include <future>
- #include <sstream>
- #include <string>
- using namespace std;
- struct Stats {
- map<string, int> word_frequences;
- void operator+=(const Stats& other) {
- for(const auto& [word,freq]:other.word_frequences){
- word_frequences[word]+=freq;
- }
- }
- };
- using KeyWords = set<string, less<>>;
- Stats asyncExploreKeyWords(const KeyWords& key_words, istream& input) {
- Stats word_freqs;
- future<Stats> asyncWordsFreq = async([&input,&key_words]{
- Stats word_freqs;
- for(string word;!input.eof();input>>word){
- if (key_words.count(word))
- ++(word_freqs.word_frequences[word]);
- }
- return word_freqs;
- });
- return asyncWordsFreq.get();
- }
- Stats ExploreKeyWords(const KeyWords& key_words, istream& input) {
- Stats word_freqs;
- for (int core=0;core<2;++core){
- word_freqs+=asyncExploreKeyWords(key_words,input);
- }
- return word_freqs;
- }
- 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