Advertisement
chevengur

СПРИНТ №1 | Структуры и классы | Урок 6: Методы классов 3/5

Sep 19th, 2023
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <set>
  4. #include <string>
  5. #include <utility>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. const int MAX_RESULT_DOCUMENT_COUNT = 5;
  11.  
  12. string ReadLine() {
  13.     string s;
  14.     getline(cin, s);
  15.     return s;
  16. }
  17.  
  18. int ReadLineWithNumber() {
  19.     int result = 0;
  20.     cin >> result;
  21.     ReadLine();
  22.     return result;
  23. }
  24.  
  25. vector<string> SplitIntoWords(const string& text) {
  26.     vector<string> words;
  27.     string word;
  28.     for (const char c : text) {
  29.         if (c == ' ') {
  30.             if (!word.empty()) {
  31.                 words.push_back(word);
  32.                 word.clear();
  33.             }
  34.         }
  35.         else {
  36.             word += c;
  37.         }
  38.     }
  39.     if (!word.empty()) {
  40.         words.push_back(word);
  41.     }
  42.  
  43.     return words;
  44. }
  45.  
  46.  
  47. class SearchServer {
  48. public:
  49.  
  50.     void AddDocument(int document_id, const string& document) {
  51.         const vector<string> words = SplitIntoWordsNoStop(document, stop_words_);
  52.         documents_.push_back({ document_id, words });
  53.     }
  54.  
  55.     set<string> SetStopWords(const string& text) {
  56.         set<string> stop_words;
  57.         for (const string& word : SplitIntoWords(text)) {
  58.             stop_words_.insert(word);
  59.         }
  60.         return stop_words_;
  61.     }
  62.  
  63. private:
  64.     struct DocumentContent {
  65.         int id = 0;
  66.         vector<string> words;
  67.     };
  68.  
  69.     vector<string> SplitIntoWordsNoStop(const string& text, const set<string>& stop_words) {
  70.         vector<string> words;
  71.         for (const string& word : SplitIntoWords(text)) {
  72.             if (stop_words.count(word) == 0) {
  73.                 words.push_back(word);
  74.             }
  75.         }
  76.         return words;
  77.     }
  78.  
  79.     vector<DocumentContent> documents_;
  80.     set<string> stop_words_;
  81. };
  82.  
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement