Advertisement
chevengur

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

Sep 1st, 2023 (edited)
213
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. int main() {
  47.     // Read stop words
  48.     string stop_words_joined;
  49.     getline(cin, stop_words_joined);
  50.     const set<string> stop_words = ParseStopWords(stop_words_joined);
  51.    
  52.     // Read query
  53.     string query;
  54.     getline(cin, query);
  55.     const vector<string> query_words = ParseQuery(query, stop_words);
  56.    
  57.     for (const string& word : query_words) {
  58.         cout << '[' << word << ']' << endl;
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement