Advertisement
chevengur

CПРИНТ № 1 | Лямбда-функции | Урок 4: Захват переменных по ссылке

Sep 27th, 2023 (edited)
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <map>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. int CountAndAddNewDogs(const vector<string>& new_dogs, const map<string, int>& max_amount, map<string, int>& shelter) {
  10.     return count_if(new_dogs.begin(), new_dogs.end(), [&max_amount, &shelter](const string& name) {
  11.         int& current_dog = shelter[name];
  12.         if (current_dog < max_amount.at(name)) {
  13.             ++current_dog;
  14.             return true;
  15.         }
  16.         else
  17.             return false;
  18.         });
  19. }
  20.  
  21. int main() {
  22.     map<string, int> shelter{
  23.         {"shepard"s, 1},
  24.         {"corgie"s, 3},
  25.     };
  26.  
  27.     map<string, int> max_amount{
  28.         {"shepard"s, 2},
  29.         {"corgie"s, 3},
  30.         {"shiba inu"s, 1},
  31.     };
  32.  
  33.     cout << CountAndAddNewDogs({ "shepard"s, "shiba inu"s, "shiba inu"s, "corgie"s }, max_amount, shelter) << endl;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement