Advertisement
chevengur

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

Sep 1st, 2023
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <set>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. vector<string> SplitIntoWords(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.         }
  18.         else {
  19.             word += c;
  20.         }
  21.     }
  22.     if (!word.empty()) {
  23.         words.push_back(word);
  24.     }
  25.  
  26.     return words;
  27. }
  28.  
  29. set<string>ParseStopWords(string words) {
  30.     set<string>stop_words;
  31.     for (const auto& word : SplitIntoWords(words)) {
  32.         stop_words.insert(word);
  33.     }
  34.     return stop_words;
  35. }
  36.  
  37. vector<string>ParseQuery(string words, set<string>stop_words) {
  38.     vector<string>query_words;
  39.     for (const auto& word : SplitIntoWords(words)) {
  40.         if (stop_words.count(word) == 0) {
  41.             query_words.push_back(word);
  42.         }
  43.     }
  44.     return query_words;
  45. }
  46.  
  47. int main() {
  48.     /* Считайте строку со стоп-словами */
  49.     string stop_words;
  50.     getline(cin, stop_words);
  51.     auto get_stop_words = ParseStopWords(stop_words);
  52.  
  53.     // Считываем строку-запрос
  54.     string query;
  55.     getline(cin, query);
  56.     auto get_query_words = ParseQuery(query, get_stop_words);
  57.  
  58.     // Выведите только те слова, которых нет среди стоп-слов
  59.     for (string word : get_query_words) {
  60.         cout << '[' << word << ']' << endl;
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement