Advertisement
chevengur

СПРИНТ № 1 | Лямбда-фукнции | Урок 3: Захват переменных по значению

Sep 26th, 2023
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. string ReadLine() {
  9.     string s;
  10.     getline(cin, s);
  11.     return s;
  12. }
  13.  
  14. int ReadLineWithNumber() {
  15.     int result;
  16.     cin >> result;
  17.     ReadLine();
  18.     return result;
  19. }
  20.  
  21. vector<string> SplitIntoWords(const string& text) {
  22.     vector<string> words;
  23.     string word;
  24.     for (const char c : text) {
  25.         if (c == ' ') {
  26.             if (!word.empty()) {
  27.                 words.push_back(word);
  28.                 word.clear();
  29.             }
  30.         }
  31.         else {
  32.             word += c;
  33.         }
  34.     }
  35.     if (!word.empty()) {
  36.         words.push_back(word);
  37.     }
  38.  
  39.     return words;
  40. }
  41.  
  42. int main() {
  43.     const int queryCount = ReadLineWithNumber();
  44.  
  45.     /*Проблема в коде заключается в том, что он не учитывает возможное наличие пробелов в конце строк в векторе queries. При чтении строк с клавиатуры, конец строки ('\n') остается во входном буфере после считывания числа queryCount. Поэтому, когда вы считываете строки с помощью getline(cin, s), он сначала считывает пустую строку, оставшуюся после числа, и только затем начинает считывать фактические строки запросов.*/
  46.  
  47.     cin.ignore(); // Очистка символа новой строки после числа
  48.  
  49.     vector<string> queries(queryCount);
  50.     for (string& query : queries) {
  51.         query = ReadLine();
  52.     }
  53.     const string buzzword = ReadLine();
  54.  
  55.     cout << count_if(queries.begin(), queries.end(), [buzzword](const string& query) {
  56.         const vector<string> query_words = SplitIntoWords(query);
  57.         return count(query_words.begin(), query_words.end(), buzzword) != 0;
  58.         });
  59.     cout << endl;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement