shushus

Спринт 1. Захват переменных по ссылке.

Dec 7th, 2022
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include <map>
  2. #include <string>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. int CountAndAddNewDogs(const vector<string>& new_dogs, const map<string, int>& max_amount,
  10.                        map<string, int>& shelter) {
  11.     //...
  12.     return count_if(begin(new_dogs), end(new_dogs), [&max_amount, &shelter](string dog) {
  13.                     if(max_amount.at(dog) > shelter[dog]) {
  14.                         return bool(++shelter[dog]);
  15.                     }else{
  16.                         return false;
  17.                     }      
  18.                 });
  19. }
  20.  
  21. int main() {
  22.     map<string, int> shelter = {{"landseer"s, 1}, {"otterhound"s, 2}, {"pekingese"s, 2}, {"pointer"s, 3}};
  23.     const map<string, int> max_amount = {{"landseer"s, 2}, {"otterhound"s, 3}, {"pekingese"s, 4},
  24. {"pointer"s, 7}};
  25.     const vector<string> new_dogs = {"landseer"s, "otterhound"s, "otterhound"s, "otterhound"s, "pointer"s};
  26.     cout<<CountAndAddNewDogs(new_dogs, max_amount, shelter)<<endl;
  27.     return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment