Advertisement
chevengur

Вводный курс: основы C++ | Урок 4: Константные ссылки

Sep 1st, 2023
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. vector<string> SplitIntoWords(const string& text) {
  9.     vector<string> words;
  10.     string word;
  11.     for (const char& c : text) {
  12.         if (c == ' ') {
  13.             if (!word.empty()) {
  14.                 words.push_back(word);
  15.                 word.clear();
  16.             }
  17.         } else {
  18.             word += c;
  19.         }
  20.     }
  21.     if (!word.empty()) {
  22.         words.push_back(word);
  23.     }
  24.  
  25.     return words;
  26. }
  27.  
  28. set<string> ParseStopWords(const string& text) {
  29.     set<string> stop_words;
  30.     for (const string& word : SplitIntoWords(text)) {
  31.         stop_words.insert(word);
  32.     }
  33.     return stop_words;
  34. }
  35.  
  36. vector<string> ParseQuery(const string& text, const set<string>& stop_words) {
  37.     vector<string> words;
  38.     for (const string& word : SplitIntoWords(text)) {
  39.         if (stop_words.count(word) == 0) {
  40.             words.push_back(word);
  41.         }
  42.     }
  43.     return words;
  44. }
  45.  
  46.  
  47. int main() {
  48.     // Read stop words
  49.     string stop_words_joined;
  50.     getline(cin, stop_words_joined);
  51.     set<string> stop_words = ParseStopWords(stop_words_joined);
  52.    
  53.     // Read query
  54.     string query;
  55.     getline(cin, query);
  56.     vector<string> query_words = ParseQuery(query, stop_words);
  57.    
  58.     for (const string& word : query_words) {
  59.         cout << '[' << word << ']' << endl;
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement