Seredenko-V

Удаление дубликатов в конце функции

Aug 2nd, 2022
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. tuple<vector<string>, DocumentStatus> SearchServer::MatchDocument(execution::parallel_policy policy,
  2.     const string& raw_query, int document_id) const {
  3.     if (!IsValidWord(raw_query)) {
  4.         throw invalid_argument("Некорректный поисковый запрос.");
  5.     }
  6.     if (document_id < 0) {
  7.         throw out_of_range("Документа с указанным id не существует.");
  8.     }
  9.     Query query = ParseQuery(raw_query);
  10.     vector<string> matched_words(query.plus_words.size());
  11.  
  12.     vector<string>::iterator end_new_size = copy_if(policy, query.plus_words.begin(), query.plus_words.end(),
  13.         matched_words.begin(),
  14.         [this, document_id](const string& plus_word) {
  15.             return word_frequencies_in_document_.at(document_id).count(plus_word);
  16.         });
  17.     matched_words.resize(distance(matched_words.begin(), end_new_size));
  18.  
  19.     if (find_if(policy, query.minus_words.begin(), query.minus_words.end(),
  20.         [this, document_id](const string& minus_word) {
  21.             return word_frequencies_in_document_.at(document_id).count(minus_word);
  22.         }) != query.minus_words.end()) {
  23.         matched_words.clear();
  24.     }
  25.  
  26.     if (!matched_words.empty()) {
  27.         sort(policy, matched_words.begin(), matched_words.end());
  28.         vector<string>::iterator last = unique(policy, matched_words.begin(), matched_words.end());
  29.         matched_words.erase(last, matched_words.end());
  30.     }
  31.  
  32.     return { matched_words, documents_.at(document_id).status };
  33. }
Advertisement
Add Comment
Please, Sign In to add comment